Reputation: 143
I'm trying to write some code which looks at the column names and sets the column value to 0 if that column name contains a specific string. I have a dataset with columns similar to this:
date apples_sold bananas_sold pineapples_sold orange_sold
I want to update any columns with the string "apple" to make every row 0 for that column. If the column doesn't contain "apple" then the value should remain.
I tried looping through the column names but am unsure the syntax as I don't want to hardcode the specific column names.
Upvotes: 2
Views: 3617
Reputation: 150765
Try loc
update with .str.contains
:
df.loc[:, df.columns.str.contains('apple')] = 0
Upvotes: 3