[英]Detect Floating Point values in Pandas Dataframe
I have a dataframe column containing integers, floating numbers and strings.我有一个包含整数、浮点数和字符串的 dataframe 列。 I want to process this column depending on what type of data is present in a particular record.
我想根据特定记录中存在的数据类型来处理此列。
Now the problem is that, I am able to separate out integer records by Series.str.isnumeric() call, but floating numbers return False here.现在的问题是,我可以通过 Series.str.isnumeric() 调用分离出 integer 记录,但浮点数在这里返回 False。 How can I separate ints & floats together.
如何将整数和浮点数分开。 Here is a basic code:
这是一个基本的代码:
import numpy as np
import pandas as pd
d = {'A' : ['1234', '12.16', '1234m']}
df= pd.DataFrame(d)
df.A.str.isnumeric()
I get [True False False] as of now, I expect to get [True, True, False].到目前为止,我得到 [True False False],我希望得到 [True, True, False]。
Use pd.to_numeric
with argument errors="coerce"
and check which values come out not NaN
:使用带有参数
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
If you want to use str.isnumeric
, pandas does not automatically recognizes the .
如果要使用
str.isnumeric
, pandas 不会自动识别.
as a decimal, so we have to replace it:作为小数,所以我们必须替换它:
df['A'].str.replace('\.', '').str.isnumeric()
0 True
1 True
2 False
Name: A, dtype: bool
If I think ahead and what you want to do, you can write a try except
to convert each element to it's type without losing any rows to NaN
:如果我提前考虑并且你想做什么,你可以写一个
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
If we then check the types per value, we see it's mixed type now:如果我们然后检查每个值的类型,我们现在看到它是混合类型:
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.