I have a 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.
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]
CodePudding user response:
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)
CodePudding user response:
You get that error because the column names in your loop are not the same as those in df
.
Now about your particular problem, you can create a boolean mask and use it to filter 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.extend(ratings.tolist())