I have a problem. I want to remove all rows that are not a number in the column ['a','b']
. I have tried this but my approach does not work very well.
Dataframe
a b c
0 0.1 10 x
1 0.5 5 y
2 10 / 5 60 z
3 9.0 12 w
4 125 a w
Code
import uuid
import pandas as pd
df = pd.DataFrame(
{'a': [0.1,0.5,'10 / 5', 9.0, 125],
'b': [10, 5, 60, 12, 'a'],
'c': ['x', 'y', 'z', 'w', 'w']
})
print(df)
df['id'] = df.apply(lambda x: uuid.uuid4().int, axis=1)
df_ = df[['a', 'b', 'id']].apply(lambda x: pd.to_numeric(x, errors='coerce')).dropna()
df.merge(df_, left_on=['id'], right_on=['id'], how='inner')
What I want
a b c
0 0.1 10 x
1 0.5 5 y
3 9.0 12 w
CodePudding user response:
You could just do
out = df[~df[['a', 'b', 'id']].apply(lambda x: pd.to_numeric(x, errors='coerce')).isna().any(1)]
CodePudding user response:
what about that
new_df = df[list(df.a.str.isnumeric() != False) and list(df.a.str.isnumeric() != False)]
a b c
0 0.1 10 x
1 0.5 5 y
3 9.0 12 w
4 125 a w
I am sure there is a nicer version doing this, but isnumeric() doesn't work as expected (by my)