Home > OS >  how to select data that contains multiple row from dataframe
how to select data that contains multiple row from dataframe

Time:06-16

i wrote the command bellow :

pd.DataFrame(df.groupby(['CHAUFFEUR','comportement de conduit'])['comportement de conduit'].count())

i get the dataframe bellow :

CHAUFFEUR comportement de conduit comportement de conduit
ABBAD DJELLOUL bad driving behaviour(reduce your acceleration) 1
ABBOU ABD EL KADER bad driving behaviour 5
bad driving behaviour(reduce your acceleration,increase your speed) 14
... ... ...
ZIANE ABDELKADER good driving behaviour(try to reduce acceleration) 18
normal driving behaviour 24
very good driving behaviour 4

now i need to select for each CHAUFFEUR all numbers of his comportement de conduit for example for the second CHAUUFEUR named ABBOU ABD EL KADER i need to select only :

bad driving behaviour 5
bad driving behaviour(reduce your acceleration) 41

so how can i do it please

CodePudding user response:

fill the null values with forward fill to fill in the blank values.

make the empty cell as np.nan

df['CHAUFFEUR'] = df['CHAUFFEUR'].str.strip().replace("", np.nan)
df['CHAUFFEUR'] = df.fillna(method='ffill')

OR

df['name'] = df['name'].str.strip().replace("", np.nan).fillna(method='ffill')

CodePudding user response:

If you use str.contains can find the answer

df = df[df['comportement de conduit'].str.contains('bad driving behaviour')]

This will show you a row for the included string.

I hope you solve it well.

  • Related