包含以下数据帧:
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[0, 0, 1, 1],
'b':[0, 1, 0, 1],
'A':['w', 'x', 'y', 'z'],
'B':[True, False, True, False],
'C':[np.nan]*4}).set_index(['a', 'b'])
我想要更改C
的值,其中'a' == 0
、'A' == w
和'B' is True
。
我找到了这个解决方案:
temp = df.loc[0]
temp.loc[(temp['A'] == 'w')&(temp['B']), 'C'] = 42
伪装已经完成,但我得到以下警告:
/home/me/.virtualenvs/myenv/lib/python3.6/site-packages/pandas/core/indexing.py:477: SettingWithCopyWarning:
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
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
这给出了相同的警告,并且不做做作:
df.query("A == 'w' & B").loc[0, 'C'] = 42
有没有一种方法可以在没有警告的情况下在df
中进行更改?
转载请注明出处:http://www.bizarre-animals.com/article/20230331/2084263.html