简体   繁体   English

不能使用条件语句将 append 列值添加到列表中

[英]Can't append column values to a list using a conditional statement

I have a dataframe我有一个 dataframe

import pandas as pd
df = pd.DataFrame({'product':['shoe','shirt','pants','socks'],
                   'review_rating':[1.2,3.0,4.0,2.1],
                   'review_text':['good','bad','good','bad']})
good_reviews = []
print(df)

I want to be able to append my review_text values to the list using a conditional statement.我希望能够使用条件语句将 append my review_text 值添加到列表中。

I tried this:我试过这个:

for column in df[['reviews.rating', 'reviews.text']]:
    if df[df['reviews.rating']] <= 2.0:
        good_reviews.append(df['reviews.text'])

After trying that I got an error:尝试后我得到一个错误:

KeyError: None of [Index(['reviews.rating', 'reviews.text'], dtype='object')] are in the [columns] KeyError: [Index(['reviews.rating', 'reviews.text'], dtype='object')] 都不在 [columns] 中

import pandas as pd
df = pd.DataFrame({'product':['shoe','shirt','pants','socks'],
                   'review_rating':[1.2,3.0,4.0,2.1],
                   'review_text':['good','bad','good','bad']})

good_reviews = df.loc[df["review_rating"] <= 2.0,'review_text']

print(good_review)

You get that error because the column names in your loop are not the same as those in df .您会收到该错误,因为循环中的列名与df中的列名不同。

Now about your particular problem, you can create a boolean mask and use it to filter review_text .现在关于您的特定问题,您可以创建一个 boolean 掩码并使用它来过滤review_text

boolean_mask = df['review_rating']<=2
ratings = df.loc[boolean_mask,'review_text']

and if you already have the good_reviews list, you can extend that list by:如果您已经有了good_reviews列表,您可以通过以下方式扩展该列表:

good_reviews.extend(ratings.tolist())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM