简体   繁体   中英

How to update multiple rows and columns in pandas?

I want to update multiple rows and columns in a CSV file, using pandas

I've tried using iterrows() method but it only works on a single column.

here is the logic I want to apply for multiple rows and columns:

if(value < mean):
  value += std_dev
else:
  value -= std_dev

Here is another way of doing it,

Consider your data is like this:

  price strings value
0     1       A     a
1     2       B     b
2     3       C     c
3     4       D     d
4     5       E     f

Now lets make strings column as the index:

df.set_index('strings', inplace='True')

#Result
        price value
strings
A           1     a
B           2     b
C           3     c
D           4     d
E           5     f

Now set the values of rows C, D, E as 0

df.loc[['C', 'D','E']] = 0

#Result
        price value
strings
A           1     a
B           2     b
C           0     0
D           0     0
E           0     0

or you can do more precisely

df.loc[df.strings.isin(["C", "D", "E"]), df.columns.difference(["strings"])] = 0
df
Out[82]: 
  price strings value
0     1       A     a
1     2       B     b
2     0       C     0
3     0       D     0
4     0       E     0

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