简体   繁体   English

使用 Pandas 从 CSV 文件中提取值

[英]Extract Values from CSV File Using Pandas

I have a CSV file where the data is organized into two columns.我有一个 CSV 文件,其中数据被组织成两列。

在此处输入图片说明

How can I get the negative sentiments and positives saved in a separate csv file.如何将负面情绪和正面情绪保存在单独的 csv 文件中。 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)
df.loc[df['class'] == 'Neg'].to_csv('negative.csv', index=False)

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')

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

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

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