获取SettingWithCopyWarning:使用.replace和.drop

时间:2017-06-30 04:01:06

标签: python pandas

我正在尝试格式化我的两个数据框,并开始获得SettingWithCopyWarnings。我知道它警告我,我可能正在使用副本,但我不知道如何解决这个问题,因为我还是Pandas的新手。

这是我的代码:

Bought = df2[df2['Quantity'] > 0] 
Sold = df2[df2['Quantity'] < 0] 
Bought.drop('Unit', axis = 1, inplace = True) 
Sold.drop('Unit', axis = 1, inplace = True) 
Bought['Account'].replace(to_replace = 'F1105', value = 'On', inplace = True) 
Bought['Account'].replace(to_replace = 'F1121', value = 'Off', inplace = True) 
Sold['Account'].replace(to_replace = 'F1105', value = 'On', inplace = True) 
Sold['Account'].replace(to_replace = 'F1121', value = 'Off', inplace = True)
Bought.reset_index(drop = True, inplace = True)
Sold.reset_index(drop = True, inplace = True)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

BoughtSoldDataFrame的切片;要摆脱警告,请先:

Bought = df2[df2['Quantity'] > 0].copy() 
Sold = df2[df2['Quantity'] < 0].copy()