簡體   English   中英

循環遍歷數據框並在滿足條件時將特定列中的行附加到新列表

[英]loop through a dataframe and append rows from a specific column to a new list when the condition is met

這是我的代碼的樣子

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,我想將推文列中的一行添加到列表中

嘗試使用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)

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM