我怎么知道 pandas 数据框单元格的类型 [英] How can I know the type of a pandas dataframe cell

查看:64
本文介绍了我怎么知道 pandas 数据框单元格的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个数据框:

I have a dataframe, for example:

1
1.3
2,5
4
5

通过以下代码,我试图了解我的熊猫数据框的不同单元格的类型是什么

With the following code, I am trying to know what are the types of the different cells of my pandas dataframe:

for i in range (len(data.columns)) :
                print (" lenth of  columns : " + str(len(data.columns)) )
                for j in range (len(data[i])) :
                    data[i][j]=re.sub(r'(\d*)\.(\d*)',r'\1,\2',str(data[i][j]))
                    print(str(data[i][j]))

                    print(" est de type : "type(data[i][j]))
                    if str(data[i][j]).isdigit():
                        print(str(data[i][j]) + " contain a number  " )

问题在于,当数据框的单元格中包含点时,pandas认为这是一个字符串.因此,我使用了正则表达式,以便将点更改为逗号.

The problem is when a cell of the dataframe contain a dot, pandas thinks it is a string. So I used regex, in order to change the dot into a comma.

但是在那之后,我所有数据框单元格的类型都更改为字符串.我的问题是:我怎么知道数据框的单元格是整数还是浮点数?我已经尝试过 isinstance(x,int)

But after that, the types of all my dataframe cells changed to string. My question is: How can I know if a cell of the dataframe is an int or a float? I already tried isinstance(x, int)

edit:例如,如何使用df.apply(type)的输出来计算int和float的数量,我想知道我的列中有多少个单元格为int或float >

edit : How can I count the number of int and float, with the output of the df.apply(type) for example , I want to know how many cells of my column are int or float

我的第二个问题是,为什么当我有2.5时,数据帧给他str类型?

My second question is, why when I have 2.5 , the dataframe give him the str type ?

    0       <class 'int'>
1       <class 'str'>
2     <class 'float'>
3     <class 'float'>
4       <class 'int'>
5       <class 'str'>
6       <class 'str'>

谢谢.

推荐答案

如果您有其他类型的列,例如

If you have a column with different types, e.g.

>>> df = pd.DataFrame(data = {"l": [1,"a", 10.43, [1,3,4]]})
>>> df
           l
0          1
1          a
2      10.43
4  [1, 3, 4]

熊猫只会声明该系列的类型为 object .但是,您可以通过简单地应用 type 函数

Pandas will just state that this Series is of dtype object. However, you can get each entry type by simply applying type function

>>> df.l.apply(type)
0     <type 'int'>
1     <type 'str'>
2     <type 'float'>
4     <type 'list'>

但是,如果您的数据集具有非常不同的数据类型,则可能应该重新考虑其设计.

However, if you have a dataset with very different data types, you probably should reconsider its design..

这篇关于我怎么知道 pandas 数据框单元格的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆