dimanche 11 février 2018

Python Pandas - How to check a value in DataFrame

How do I find a missing line in the dataframe and add a new one?

The DataFrame df

    federalState    hasParking  Size
0   A               False       154
1   A               True        531
2   B               False       191
3   B               True        725
4   C               True        54
5   D               False       100
6   D               True        656

For df['federalState'] the false for C is missing

The final result should look like this

    federalState    hasParking  Size
0   A               False       154
1   A               True        531
2   B               False       191
3   B               True        725
4   C               False       89
5   C               True        54
6   D               False       100
7   D               True        656

My code for adding the new line

df.loc[-1] = ['C', 'False' , 89]  # adding a row
df.index = df.index + 1  # shifting index
df = too.sort_values(by=['federalState'])  # sorting by index

But how do I find out that the line is missing? My if-statement does not work

if ((data['federalState']=='C) and (data['hasParking']=='True')).any():

Aucun commentaire:

Enregistrer un commentaire