Python 函数检查对象数据类型列值是浮点值还是字符串
我正在尝试编写一个函数,它与excel中的isnumber[column]函数相等 数据集:Python 函数检查对象数据类型列值是浮点值还是字符串,python,python-3.x,python-2.7,user-defined-types,isinstance,Python,Python 3.x,Python 2.7,User Defined Types,Isinstance,我正在尝试编写一个函数,它与excel中的isnumber[column]函数相等 数据集: feature1 feature2 feature3 123 1.07 1 231 2.08 3 122 ab 4 111 3.04 6 555 cde 8 feature1: integer dtype feature2: object dtype feature3: int
feature1 feature2 feature3
123 1.07 1
231 2.08 3
122 ab 4
111 3.04 6
555 cde 8
feature1: integer dtype
feature2: object dtype
feature3: integer dtype
我试过这段代码
for item in df.feature2.iteritems():
if isinstance(item, float):
print('yes')
else:
print('no')
我得到的结果是
no
no
no
no
no
yes
yes
no
yes
no
但我想知道结果
no
no
no
no
no
yes
yes
no
yes
no
当我尝试检查单个feature2值的类型时,我看到的是
type(df.feature2[0]) = str
type(df.feature2[1]) = str
type(df.feature2[2]) = str
type(df.feature2[3]) = str
type(df.feature2[4]) = str
But clearly 0,1,3 should be shown as float, but they show up as str
我做错了什么 试试这个:
for i in range(len(df["feature2"])):
test = df.loc[i,"feature2"]
if isinstance(test, float):
print('yes')
else:
print('no')
这是因为
iteritems()
返回一个元组,它是(索引,值)
。
例如,您试图检查(0,1.07)
或(1,2.08)
是否为float类型,当然不是
如果将
df.feature2.iteritems()
更改为df.feature2.values
:)iteritems正在返回一个元组,((123,'1.07'),1.07)
,并且由于要循环每个值,请尝试下面的代码。
您只需删除.iteritems()
,它就会像一个符咒一样工作
df['feature2']=[1.07,2.08,'ab',3.04,'cde']
for item in df.feature2:
if isinstance(item,float):
print('yes')
else:
print('no')
以下是您的输出:
yes
yes
no
yes
no
您可以这样做:
from pandas import DataFrame as df
columns = ['feature1', 'feature2', 'feature3']
data = [[123, 1.07, 1],
[231, 2.08, 3],
[122, 'ab', 4],
[111, 3.04, 6],
[555, 'cde', 8]]
df_ = df(data, columns=columns)
types = []
for k in df_:
a = set(type(m) for m in df_[k])
if len(a) > 1:
types.append({k: 'object'})
else:
types.append({k: str(list(a)[0].__name__)})
print(types)
输出:
[{'feature1': 'int'}, {'feature2': 'object'}, {'feature3': 'int'}]
我认为这里有两件事需要考虑:
Dict
vsDataFrame
.iteritems()
/.items()
是字典的方法,而如果处理数据类型(并根据您提供的数据判断),则很可能要经历一个数据帧,在该数据帧中,不需要使用.iteritems()
方法来循环每个值。旁注,.iteritems()
已被Python淘汰,取而代之的是.items()
(请参阅讨论:)
第2点:
使用numpy或Pandas时,导入到数据帧中的值的数据类型称为dtypes
。这些需要与Python中的直接比较区别开来,Python将其称为type
。您应该使用“数据类型”标题下的表格将数据类型
映射到类型
(参考:)
现在,为了回答您的问题,这段代码应该可以解决您的问题:
import pandas as pd
columns = ['feature1', 'feature2', 'feature3']
data = [[123, 1.07, 1],
[231, 2.08, 3],
[122, 'ab', 4],
[111, 3.04, 6],
[555, 'cde', 8]]
df = pd.DataFrame(data, columns=columns)
for value in df.feature2:
if isinstance(value,float):
print('yes')
else:
print('no')
请记住,这只是测试浮点数-如果您想要任何数字、浮点数或整数,您必须将第三行更改为if isinstance(test,float)或isinstance(test,int):如果有帮助,请注意接受并投票表决答案。谢谢:)@SaiSumanth你能告诉我有什么错误吗?我包括了我用于测试的数据帧创建行,它是从我开始工作的。另外,我正在使用python3it,实际上我的特性值是“floatstring”类型,而不是float类型。谢谢