满足条件时遍历数据框并将行从特定列追加到新列表

时间:2020-02-28 00:52:14

标签: python pandas dataframe

这是我的代码的样子

pos_list = []
for i in range(len(df)): 
    if df.loc[i, "sentiment"] == 1:
        pos_list = df.loc[i, "tweets"].tolist()

    else:
        pass
print(pos_list)

所以如果情绪== 1,我想在tweets列中添加一行到列表中

1 个答案:

答案 0 :(得分:1)

尝试使用append方法:

pos_list = []
    for i in range(len(df)): 
    if df.loc[i, "sentiment"] == 1:
        pos_list.append(df.loc[i, "tweets"])

    else:
        pass
print(pos_list)

希望这会有所帮助。