[英]Detect Floating Point values in Pandas Dataframe
我有一个包含整数、浮点数和字符串的 dataframe 列。 我想根据特定记录中存在的数据类型来处理此列。
现在的问题是,我可以通过 Series.str.isnumeric() 调用分离出 integer 记录,但浮点数在这里返回 False。 如何将整数和浮点数分开。 这是一个基本的代码:
import numpy as np
import pandas as pd
d = {'A' : ['1234', '12.16', '1234m']}
df= pd.DataFrame(d)
df.A.str.isnumeric()
到目前为止,我得到 [True False False],我希望得到 [True, True, False]。
使用带有参数errors="coerce"
的pd.to_numeric
并检查哪些值not NaN
:
pd.to_numeric(df['A'],errors='coerce').notna()
0 True
1 True
2 False
Name: A, dtype: bool
如果要使用str.isnumeric
, pandas 不会自动识别.
作为小数,所以我们必须替换它:
df['A'].str.replace('\.', '').str.isnumeric()
0 True
1 True
2 False
Name: A, dtype: bool
如果我提前考虑并且你想做什么,你可以写一个try except
将每个元素转换为它的类型而不丢失任何行到NaN
:
def convert_numeric(x):
try:
return pd.to_numeric(x)
except:
return x
df['A'].apply(convert_numeric)
0 1234
1 12.16
2 1234m
Name: A, dtype: object
如果我们然后检查每个值的类型,我们现在看到它是混合类型:
df['A'].apply(convert_numeric).apply(type)
0 <class 'numpy.int64'>
1 <class 'numpy.float64'>
2 <class 'str'>
Name: A, dtype: object
def my_func(x):
try:
float(x)
except ValueError:
return False
return True
df['A'].apply(my_func)
0 True
1 True
2 False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.