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] = 0df['var1001'][df['type']==7] = 0df['var1002'][df['type']==7] = 0...df['var1099'][df['type']==7] = 0Is 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- @cs95 how is this a duplicate of the question posted more than 4 years after?ayorgo– ayorgo2019-04-25 16:03:42 +00:00CommentedApr 25, 2019 at 16:03
- @ayorgo duplicates do not only have to be fixed based on chronological ordering. IMO both of the answers in the other question do a good (better) job of answering the question than the answer below (that uses a deprecated function to add to things).cs95– cs952019-04-25 16:54:47 +00:00CommentedApr 25, 2019 at 16:54
- @cs95 I bet it'd spark quite a debate if mentioned on meta. Oh, wait...meta.stackexchange.com/a/147651 Seems legit then, although making the banner misleading. Btw, the reason I noticed is because of the reference frompandas.pydata.org/pandas-docs/stable/user_guide/…ayorgo– ayorgo2019-04-25 17:56:34 +00:00CommentedApr 25, 2019 at 17:56
1 Answer1
df.ix[df.type==7, ['var1001', 'var1002']] = 0If you're doing it on all columns, you can just dodf.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] = 03 Comments
var10_cols = [col for col in df.columns if isinstance(col, basestring) and col.startswith('var10')]Explore related questions
See similar questions with these tags.


