0

My sample dataset. My original dataset contains 40,000 rows and 4 columns.

enter image description here

I want to extract rows where all sentiment columns are the same (either all positive like Document 2, 5, and 6 or all negative like Document 3).

How can I do this by using python?

Thank you for your suggestions and advice.

2
  • did you solve it or not? Commented Oct 23, 2020 at 4:19
  • I made some correction. I used this: negative_df = df[df['Sentiment_B']<0]. But this only extract on the basis of Sentiment_B column. I want something which extract rows where Sentiment_A, Sentiment_B and Sentiment_C all are negative or positive. Commented Oct 23, 2020 at 9:37

1 Answer 1

1

Based on column condition

Thanks, David for pointing out the type changing.

df['Sentiment_B'] = pd.to_numeric(df['Sentiment_B'], errors='coerce')
positive_df = df[df['sentiment_B'>0]] 
nagative_df = df[df['sentiment_B'<0]]

More details https://www.geeksforgeeks.org/selecting-rows-in-pandas-dataframe-based-on-conditions/

Sign up to request clarification or add additional context in comments.

2 Comments

I got the error: TypeError: '<' not supported between instances of 'str' and 'int'.
try this first: df['Sentiment_B'] = pd.to_numeric(df['Sentiment_B'], errors='coerce')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.