Python 在查询返回的索引处更新数据帧

Python 在查询返回的索引处更新数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我希望查询返回一个视图,这样我就可以修改字段而不产生此错误 SettingWithCopyWarning:正在尝试在副本上设置值 从数据帧切片。尝试使用.loc[行索引器、列索引器]= 取而代之的是价值观 请参阅文档中的注意事项: 我有一个多行(长)查询q,我在这里缩写为: df1 = pd.Dataframe(...) q = "tcpstream==1 and ipsrc=='10.0.0.1' and sport==5201" df = df1.query(q) df['des

我希望查询返回一个视图,这样我就可以修改字段而不产生此错误

SettingWithCopyWarning:正在尝试在副本上设置值 从数据帧切片。尝试使用.loc[行索引器、列索引器]= 取而代之的是价值观

请参阅文档中的注意事项:

我有一个多行(长)查询
q
,我在这里缩写为:

df1 = pd.Dataframe(...)
q = "tcpstream==1  and ipsrc=='10.0.0.1' and sport==5201"
df = df1.query(q)    
df['dest'] = "toto"   # <--- this generates the warning/error
df1=pd.Dataframe(…)
q=“tcpstream==1和ipsrc==10.0.0.1”和sport==5201”
df=df1.query(q)
df['dest']=“toto”#用于查询,而不是设置值。如果要将字符串查询用作掩码,可以计算索引并输入:

df.loc[df.query(q).index, 'dest'] = 'toto'