当值不存在时(使用 inplace=T ),从 df 中删除行会引发SettingWithCopyWarning 错误(Pandas、Python)?
我试图根据数据帧的特定值删除一些行:
dd = {'ae': pd.DataFrame(dict(a=[1,2,4], b=[4,5,6])),
'be': pd.DataFrame(dict(a=[13,21,413], b=[456,54,62]))}
def test_remove(dd, val):
test = dd.get('ae')
test.drop(test.loc[test['a']==val].index, inplace=True)
test_remove(dd, 24242)
但我收到以下错误:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py:4906: SettingWithCopyWarning:正在尝试在某个副本上设置一个值 从 DataFrame 中切片
请告知这里出了什么问题?我有一个数据帧字典,我试图进入每个数据帧并根据特定的列值删除一行。
I am trying to drop some rows based on a specific value of a dataframe:
dd = {'ae': pd.DataFrame(dict(a=[1,2,4], b=[4,5,6])),
'be': pd.DataFrame(dict(a=[13,21,413], b=[456,54,62]))}
def test_remove(dd, val):
test = dd.get('ae')
test.drop(test.loc[test['a']==val].index, inplace=True)
test_remove(dd, 24242)
But I am getting the following error:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py:4906:
SettingWithCopyWarning: A value is trying to be set on a copy of a
slice from a DataFrame
Please advise what is wrong here? I have a dictionary of dataframes, I am trying to get into each of them and remove a row based on a specific column value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其就地进行删除,不如执行以下操作:
Rather than doing the remove in place, it would be better to do something like this: