如果满足此列的条件,则从另一列获取值

时间:2018-05-10 00:04:27

标签: python pandas

我试图从列中获取值' count'并根据列“情绪”的条件将它们分组到相应的列表中。满足了。

表:

    hashtag   sentiment count
0   audi      negative  116
1   audi      neutral   885
2   audi      positive  786
6   bmw       negative  237
7   bmw       neutral   1266
8   bmw       positive  1126
32  ferrari   negative  200
33  ferrari   neutral   1175
34  ferrari   positive  910

期望的结果:

sent_pos  = (786, 1126, 910)
sent_neg = (116, 237, 200)

我写过这样的话:

for j in car_df.iterrows():
    if car_df['sentiment'] == 'positive' :
        sent_pos = car_df.iloc[row,2]
    elif car_df['sentiment'] == 'negative' :
        sent_neg = car_df.iloc[row,2]
    else:
        sent_neutral == car_df.iloc[row,2]
return sent_pos, sent_neg, sent_neutral

但是出现了这个错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

感谢任何帮助

谢谢

1 个答案:

答案 0 :(得分:0)

您可以简单地使用$ awk 'NR==FNR{a[$1];next} $1 in a{delete a[$1]; print}' reference file ,例如:

groupby

或者只是布尔选择:

g = car_df.groupby('sentiment')
return tuple(g.get_group(s)['count'].tolist() for s in ['positive', 'negative', 'neutral'])