1. 程式人生 > >pandas 數據處理遇到的問題

pandas 數據處理遇到的問題

ima es2017 frame all assign emp () png col

數據為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 數據處理遇到的問題