Sunday, 3 June 2018

How to delete values in a pandas df column

I have a pandas df that I want to manipulate so it's ordered. So for the df below, I'd like ['I'] to be ordered. So values would read 10-50. I have 2 options to do this;

1) Try to delete values in Column ['G'] or ['H']. So if values are == X then delete.

2) Try to merge values in the same Columns when they are == X

import pandas as pd

d = pd.DataFrame({
        'J' : [10,'B','C','C',50],
        'I' : ['B',20,30,40,'C'],
        'H' : ['X','A','C','B','X'],         
        'G' : ['X', 'B', 'A','B','X'],                                 
        })

Output:

   G  H   I   J
0  X  X   B  10
1  B  A  20   B
2  A  C  30   C
3  B  B  40   C
4  X  X   C  50

Option 1 is we delete X from Column H and the intended output would be:

   G  H   I   J
0  X  B  10
1  B  A  20   B
2  A  C  30   C
3  B  B  40   C
4  X  C  50

Option 2 is we merge on X in Column G-H and the intended output would be:

   G   H   I   J
0  XX  B  10
1  B   A  20   B
2  A   C  30   C
3  B   B  40   C
4  XX  C  50

I have played around with df = df.drop(df.H == 'X') but this deletes the whole row.



from How to delete values in a pandas df column

No comments:

Post a Comment