pythonpandasdataframe

Trying to remove all rows without a numeric value in a column using Python Pandas


Consider:

       age
0       55
1       45
2       58
4      N/A

I need to remove all the rows that doesn't contain numeric values in column 'age' above, given the dataframe example.

The expected output is given below

       age
0       55
1       45
2       58

Solution

  • Try this

    import pandas as pd
    import numpy as np
    data = {
    "age": [0, 55, 1,55,4,'N/A',5]
    
    }
    df = pd.DataFrame(data)
    df=df[df['age'].apply(lambda x: type(x) in [int, np.int64, 
    float, np.float64])]
    
    print(df)