john doe
john doe

Reputation: 9660

Extract Values from CSV File Using Pandas

I have a CSV file where the data is organized into two columns.

enter image description here

How can I get the negative sentiments and positives saved in a separate csv file. My code is below:

import pandas as pd 

df = pd.read_csv('~/Downloads/epinions3.csv')

df_neg = df.loc[['Neg']] # get all negative sentiments and then save to a file 

df_pos = # get all positive sentiments and save to a file 

print(df_neg)

Upvotes: 0

Views: 1032

Answers (3)

Prashant Kumar
Prashant Kumar

Reputation: 2092

Try this :

import pandas as pd 

df = pd.read_csv('~/Downloads/epinions3.csv')

df_neg = df.loc[df['class'] == 'Neg'] # get all negative sentiments

df_pos = df.loc[df['class'] == 'Pos'] # get all positive sentiments

df_neg.to_csv("neg.csv")    # write neg to csv
df_pos.to_csv("pos.csv")    # write pos to csv

Upvotes: 0

Mahesh Sinha
Mahesh Sinha

Reputation: 91

You can use below two lines in order to filter and save files:

df[df['class']==Neg].to_csv('Neg.csv')

df[df['class']==Pos].to_csv('Pos.csv')

Upvotes: 0

coco18
coco18

Reputation: 1085

df.loc[df['class'] == 'Neg'].to_csv('negative.csv', index=False)

Upvotes: 1

Related Questions