我面临的问题与documentation中解释的问题非常相似。此代码有效,但会发出警告:
In [296]: dfb = DataFrame({'a' : ['one', 'one', 'two',
.....: 'three', 'two', 'one', 'six'],
.....: 'c' : np.arange(7)})
.....:
# This will show the SettingWithCopyWarning
# but the frame values will be set
In [297]: dfb['c'][dfb.a.str.startswith('o')] = 42
无论如何都要在过滤器中实现相同的行为而不引发警告吗?
答案 0 :(得分:1)
使用loc
,将布尔条件放在方括号[]
中,将逗号列放在逗号后面,这样就不会显示chained indexing:
In [40]:
dfb.loc[dfb.a.str.startswith('o'),'c'] = 42
dfb
Out[40]:
a c
0 one 42
1 one 42
2 two 2
3 three 3
4 two 4
5 one 42
6 six 6