在类型为float或特定类型的Pandas中查找数据框的所有列? [英] Find all columns of dataframe in Pandas whose type is float, or a particular type?

查看:224
本文介绍了在类型为float或特定类型的Pandas中查找数据框的所有列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df,其中有一些类型为float64的列,而其他的则是对象.由于混合的性质,我不能使用

I have a dataframe, df, that has some columns of type float64, while the others are of object. Due to the mixed nature, I cannot use

df.fillna('unknown') #getting error "ValueError: could not convert string to float:"

因为错误发生在类型为float64的列上(这是一个令人误解的错误消息!)

as the error happened with the columns whose type is float64 (what a misleading error message!)

所以我希望我可以做类似的事情

so I'd wish that I could do something like

for col in df.columns[<dtype == object>]:
    df[col] = df[col].fillna("unknown")

所以我的问题是,是否可以在df.columns中使用任何此类过滤器表达式?

So my question is if there is any such filter expression that I can use with df.columns?

或者,我可以不太优雅地猜测:

I guess alternatively, less elegantly, I could do:

 for col in df.columns:
        if (df[col].dtype == dtype('O')): # for object type
            df[col] = df[col].fillna('') 
            # still puzzled, only empty string works as replacement, 'unknown' would not work for certain value leading to error of "ValueError: Error parsing datetime string "unknown" at position 0" 

我还想知道为什么在上面的代码中用'unknown'替换''的代码对于某些单元格有效,但由于单元格错误"ValueError:解析日期时间字符串"unknown"在位置0时出错而失败"

I also would like to know why in the above code replacing '' with 'unknown' the code would work for certain cells but failed with a cell with the error of "ValueError: Error parsing datetime string "unknown" at position 0"

非常感谢!

Y

推荐答案

您可以使用dtypes属性查看所有列的dtype:

You can see what the dtype is for all the columns using the dtypes attribute:

In [11]: df = pd.DataFrame([[1, 'a', 2.]])

In [12]: df
Out[12]: 
   0  1  2
0  1  a  2

In [13]: df.dtypes
Out[13]: 
0      int64
1     object
2    float64
dtype: object

In [14]: df.dtypes == object
Out[14]: 
0    False
1     True
2    False
dtype: bool

要访问对象列:

In [15]: df.loc[:, df.dtypes == object]
Out[15]: 
   1
0  a

我认为使用起来最明确(我不确定确定在这里可以正常使用):

I think it's most explicit to use (I'm not sure that inplace would work here):

In [16]: df.loc[:, df.dtypes == object] = df.loc[:, df.dtypes == object].fillna('')

说,我建议您使用 NaN丢失数据.

这篇关于在类型为float或特定类型的Pandas中查找数据框的所有列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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