Learn how to extract rows from string columns with values that can be converted to integers or floating-point numbers in Pandas DataFrame.
Could you tell me?
By using the series str.isidigit(), we were able to extract rows that could be converted to integers as follows:
How do I determine if I can convert to decimal places?
import pandas as pd
df=pd.DataFrame(data=[
{'aa': 'Error', 'bb': 'BB01'},
{'aa':'10.5', 'bb':'BB02',
{'aa':'20', 'bb':'BB02',
])
display('Input')
display(df.info())
display(df)
display('Output')
df_out = df [df['aa'].str.isdigit()]
display(df_out)
display('Expected Results')
df_expected=pd.DataFrame(data=[
{'aa':'10.5', 'bb':'BB02',
{'aa':'20', 'bb':'BB02',
])
display(df_expected)
In addition, Python's standard feature seemed difficult to determine whether it could be converted from a string to a decimal.
https://neko-py.com/python-type-judge
>>>df [pd.to_numeric, errors='coerce').notna().any(axis=1)]
abb
110.5 BB02
220 BB02
© 2024 OneMinuteCode. All rights reserved.