当值不存在(使用 inplace=T)时,从 df 中删除行会引发 SettingWithCopyWarning 错误(Pandas、Python)?
Dropping rows from a df rises a SettingWithCopyWarning error (Pandas, Python) when value doesn't exist (using inplace=T)?
我正在尝试根据数据框的特定值删除一些行:
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: A value is trying to be set on a copy of a
slice from 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 remove_rows(dd, key, val, col):
dd[key] = dd[key].drop(dd[key].loc[dd[key][col]==val].index)
return dd
dd = remove_rows(dd, 'ae', 413, 'a')
我正在尝试根据数据框的特定值删除一些行:
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: A value is trying to be set on a copy of a slice from 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 remove_rows(dd, key, val, col):
dd[key] = dd[key].drop(dd[key].loc[dd[key][col]==val].index)
return dd
dd = remove_rows(dd, 'ae', 413, 'a')