I want to change the name in the name column to default
for only rows that meet a specific condition ( name == None
and weight == 1
). As you can see weight can be 10, but I do not want to change the names for weights of 10. Here is a small example dataframe:
post name weight response
blah1 None 1 yes
blah2 None 10 no
blah3 Annie 1 yes
How can I achieve this in a simple way?
快速,坚固,强大的熊猫式解决方案:
df.loc[(df.name == None) & (df.weight == 1), 'name'] = 'defaut'
You can update
the dataframe with a filled in version of the 'name'
column.
df.update(df.name[df.weight.eq(1)].fillna('default'))
df
post name weight response
0 blah1 default 1 yes
1 blah2 None 10 no
2 blah3 Annie 1 yes
np.where((df['name'] == 'None') & (df['weight'] == 1), 'default', df['name'])
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.