简体   繁体   中英

How to speed up cell increment in Pandas DataFrame?

I need to apply integer increment on a cell in a Pandas DataFrame. The code below works correctly. However, it is very slow in a loop:

df.at[index, column_name] += 2

Is there a way to speed it up?

It should be a lot simpler:

df[columns] += 2

Or, if you want to do it for every column:

df += 2

If you have a special selection of indexes to use, try this:

indexes = [0, 4, 5, 8]
columns = ['a', 'b', 'k']
df.loc[indexes, columns] += 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM