Reputation: 9660
I have a CSV file where the data is organized into two columns.
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
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
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