首页 > 解决方案 > extract values of column according to value in another columns in csv file

问题描述

Here is my file:

 id;verbatim;score
0;1; je suis beau;1
1;2; je suis laid;0
2;3;je suis merveilleux;1
3;4;je suis repugne;0

I would like to extract all sentences in column "verbatim" which have a score of 1 and all which have a score of 0 so that I have two separated files:

print(verbatim, score = 1)
id;verbatim;score
1; je suis beau;1
3;je suis merveilleux;1

and

print(verbatim, score = 0)
id;verbatim;score
2; je suis laid;0
4;je suis repugne;0

I started to write some code but I do not really think it is on the right path:

df = pd.read_csv("out.csv", na_values = ['no info', '.'], encoding='latin-    1', delimiter=';')    
m1 = df['verbatim'].eq(0)
m2 = df['critere'].eq(0)


SizePos = df[m1 & m2]
dSizeZero_PptPosf2 = df[m1 & ~m2]
SizeZero_PptZero = df[~m1]

print(SizePos)

When i print df.head() after reading the file I have :

      id                                           Verbatim   ...    Scoreneg  Scoreneu
   0   1  Je nai pas bien compris si cétait destiné à ...   ...           6813     3202
   1   2  Peut-être quil faut que je révise mes classiq...   ...       20842     3974
   2   3  ça peut donner une photographie pour dire que ...   ...        5083      384
   3   4  Je comprends bien lintérêt quil peut y avoir...   ...       11335     1132
   4   5         Jai bien compris le concept, cest clair.   ...         258       91

标签: python-3.xpandascsvparsingdictionary-comprehension

解决方案


If you intend on using pd.read_csv(), and you are interested in extracting just the sentences that meet your desired criteria, then you can do the following:

import pandas as pd

df = pd.read_csv('test.csv', sep=';')

df[df['score']==1]['verbatim'].values

df[df['score']==0]['verbatim'].values

This would give:

[' je suis beau' 'je suis merveilleux']
[' je suis laid' 'je suis repugne']

推荐阅读