EXTRACTION METHOD OF LINE CONVERSIBLE TO INTEGER/FLOATING POINT FROM DATAFrame OF PANDAS

Asked 1 years ago, Updated 1 years ago, 429 views

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)

Enter a description of the image here

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

python3 pandas

2022-11-03 08:50

1 Answers

>>>df [pd.to_numeric, errors='coerce').notna().any(axis=1)]
     abb
110.5 BB02
220 BB02


2022-11-03 08:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.