作者:是冉冉升起的冉啊 | 来源:互联网 | 2023-10-10 12:55
数据为DataFrame格式,如下:1.对每一行,FirstCab的值为空时,Weight的值乘以0.8方法一(可行):df.loc[df['FirstCab'].
数据为DataFrame格式,如下:
1.对每一行,FirstCab的值为空时,Weight的值乘以0.8
方法一(可行):df.loc[df[‘FirstCab‘].isnull(),‘Weight‘] *= 0.8
方法二(可行):df[‘Weight‘] = np.where(df[‘FirstCab‘].isnull(),df[‘Weight‘]*0.8,df[‘Weight‘])
方法三(不可行):df[df[‘FirstCab‘].isnull()][‘Weight‘] *= 0.8 或者 df.loc[df[‘FirstCab‘].isnull(),:][‘Weight‘] *= 0.8
错误提示:A value is trying to be set on a copy of a slice from a DataFrame.Try using .loc[row_indexer,col_indexer] = value instead.
方法四(不可行):df.assign(Weight=lambda x: x[‘Weight‘]*0.8 if x[‘FirstCab‘].isnull() else x[‘Weight‘])
或者df.assign(Weight=lambda x: x[‘Weight‘]*0.8 if x[‘FirstCab‘] == np.nan else x[‘Weight‘])
错误提示:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
pandas 数据处理遇到的问题