'Trying to remove all rows without a numeric value in a column using python pandas
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 dataframe example
Expected output is given bellow
age
0 55
1 45
2 58
Solution 1:[1]
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)
Solution 2:[2]
Use pd.numeric as boolean mask:
df = df.loc[pd.to_numeric(df['age'], errors='coerce').notna()]
print(df)
# Output
age
0 55
1 45
2 58
All non numeric values will be converted as NaN.
Solution 3:[3]
import pandas as pd
import numpy as np
data={"Name":["jhon","alex","lisa","maya"],"age":[10,14,np.nan,15]}
df=pd.DataFrame(data)
df.dropna()
Hope it was some help to you. dropna() method drop the rows with na value
Solution 4:[4]
The shortest solution is to use _convert(numeric=True):
df['age'] = df['age']._convert(numeric=True)
df.dropna(inplace=True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter |
| Solution 2 | Corralien |
| Solution 3 | Veena Kumari |
| Solution 4 | Phoenix |
