After performing calculations on an entire pandas dataframe, I need to go back and override variable calculations (often setting to zero) based on the value of another variable(s). Is there a more succinct/idiomatic way to perform this kind of operation?
df['var1000'][df['type']==7] = 0
df['var1001'][df['type']==7] = 0
df['var1002'][df['type']==7] = 0
...
df['var1099'][df['type']==7] = 0
Is there a pandas-y way to do something like this?
if (df['type']==7):
df['var1000'] = 0
df['var1001'] = 0
df['var1002'] = 0
...
df['var1099'] = 0
By using 'last', the last occurrence of each set of duplicated values is set on False and all others on True. By setting keep on False, all duplicates are True. To find duplicates on specific column(s), use subset .
df.ix[df.type==7, ['var1001', 'var1002']] = 0
If you're doing it on all columns, you can just do df.ix[df.type==7] = 0
. Or of course if you have a list of the columns whose values you want to replace, you can pass that list in the second slot:
columnsToReplace = ['var1001', 'var1002', ...]
df.ix[df.type==8, columnsToReplace] = 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With