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.
Try loc
update with .str.contains
:
df.loc[:, df.columns.str.contains('apple')] = 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.