我从 Excel 中读取数据并按单元格的内容进行过滤。
x200 = df['Код ответа сервера']=='200 OK'
xp = df['Код ответа сервера'].isnull()
c1 = df['Content-Type'].str.contains('text/html', na=False)
c2 = df['Content-Type'].isnull()
f1 = df[(x200 | xp) & (c1|c2) ]
此外,我想更改此过滤后的数据。没有错误,但在终端中我看到
A value is trying to be set on a copy of a slice from a DataFrame
并且数据没有保存。怎么修?
for i,(index,row) in enumerate(f1.iterrows()):
# for index, row in f1.iterrows():
u1 = str(row['Уровень 1'])
if u1!='nan':
f1.loc[index]['URL']=u1
f1.loc[index]['Title']=u1
f1.loc[index]['Category']=u1
试试这样:
如果你使用
.loc它,那么一切都应该没问题。并且不要正常使用.iterrows它,你可以不用它:虽然需要检查,但在分配时可能仍然需要使用
.loc,之前记住了位掩码,而不是立即.loc从数据帧中取出。但这仅在您想完全更改原始 df 并且如果您想要一个副本时 - 请参阅 MaxU 的答案。