python中的If条件在循环中
我在if条件的正确语法方面面临困难。我想应用这个条件来检查字符串的相等性。但我尝试这样做的方式给了我一个错误:python中的If条件在循环中,python,if-statement,pandas,Python,If Statement,Pandas,我在if条件的正确语法方面面临困难。我想应用这个条件来检查字符串的相等性。但我尝试这样做的方式给了我一个错误: AttributeError: 'str' object has no attribute 'any' for : for a in prod_comm: if prod_comm['label']==('label":"neg"}').any(): prod_pos.append(a) 或: 我想做的是: 有一个包含两列的数据框-c和label。我想根据标签
AttributeError: 'str' object has no attribute 'any'
for :
for a in prod_comm:
if prod_comm['label']==('label":"neg"}').any():
prod_pos.append(a)
或:
我想做的是:
有一个包含两列的数据框-c和label。我想根据标签类型将此数据帧划分为不同的文件,即:
label":"neg"} or label":"pos"}
我如何进行分类?我猜我使用的语法有问题,但是在网上找不到正确的语法
Example dataset:
comment label
0 need 3 bhk house "label": "neg"}
2 tried to list my property "label": "neg"}
22 i have issue with the map "label": "neg"}
24 also help with the rental agreements "label": "pos"}
37 sort option should be available. "label": "pos"}
expected output:
two datasets:
prod_pos - with the comments corresponding to label: "label": "pos"}
prod_neg - with the comments corresponding to label: "label": "neg"}
i.e.
prod_pos:
comment label
24 also help with the rental agreements "label": "pos"}
37 sort option should be available. "label": "pos"}
prod_neg:
comment label
0 need 3 bhk house "label": "neg"}
2 tried to list my property "label": "neg"}
22 i have issue with the map "label": "neg"}
谢谢你的帮助 您似乎有一个熊猫数据帧,所以可以这样做: 给定您的数据帧:
df
comment label
0 need 3 bhk house label: "neg"}
1 tried to list my property label: "neg"}
2 i have issue with the map label: "neg"}
3 also help with the rental agreements label: "pos"}
4 sort option should be available. label: "pos"}
只要做:
df_neg = df[df.label=='label: "neg"}']
df_pos = df[df.label=='label: "pos"}']
其中:
df_neg
comment label
0 need 3 bhk house label: "neg"}
1 tried to list my property label: "neg"}
2 i have issue with the map label: "neg"}
df_pos
comment label
3 also help with the rental agreements label: "pos"}
4 sort option should be available. label: "pos"}
HTH谢谢
('label:“neg”}'),您能发布代表性的数据、代码、错误和其他人可以运行的所需输出吗?any()
-您明确指出。any()
应该在'label:“neg”}'
上调用。请重新考虑一下括号的用法。我试过在没有括号的情况下使用它。它仍然给出了相同的错误注释。
df_neg
comment label
0 need 3 bhk house label: "neg"}
1 tried to list my property label: "neg"}
2 i have issue with the map label: "neg"}
df_pos
comment label
3 also help with the rental agreements label: "pos"}
4 sort option should be available. label: "pos"}