python - 按列 NAME dtype 选择列

标签 python pandas

import pandas as pd
import numpy as np
cols = ['string',pd.Timestamp('2017-10-13'), 'anotherstring', pd.Timestamp('2017-10-14')]
pd.DataFrame(np.random.rand(5,4), columns=cols)

如何只取回第二列和第四列(其数据类型为“date time.datetime”)?列内容的类型完全相同,因此 select_dtypes 没有帮助。

最佳答案

typemap结合使用:

df = df.loc[:, df.columns.map(type) == pd.Timestamp]
print (df)
   2017-10-13 00:00:00  2017-10-14 00:00:00
0             0.894932             0.502015
1             0.080334             0.155712
2             0.600152             0.206344
3             0.008913             0.919534
4             0.280229             0.951434

详细信息:

print (df.columns.map(type))
Index([                         <class 'str'>,
       <class 'pandas._libs.tslib.Timestamp'>,
                                <class 'str'>,
       <class 'pandas._libs.tslib.Timestamp'>]

print (df.columns.map(type) == pd.Timestamp)
[False  True False  True]

替代解决方案:

df1 = df.loc[:, [isinstance(i, pd.Timestamp) for i in df.columns]]
print (df1)
   2017-10-13 00:00:00  2017-10-14 00:00:00
0             0.818283             0.128299
1             0.570288             0.458400
2             0.857426             0.395963
3             0.595765             0.306861
4             0.196899             0.438231

关于python - 按列 NAME dtype 选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46728376/

相关文章:

python - 如何根据特定条件过滤重复行

python - 如何在自定义包中运行Python代码?

python - 有没有可以将正则表达式转换为 fsm 的编译器?或者可以转换成人类的话?

python - dask dataframe.persist() 是否保留下一个查询的结果?

python - 从数据帧的列切片中获取 numpy 值的最快方法是什么

python - 没有名为 parse 的模块

python - 使 kwargs 可直接访问

python - 可以用 Pandas 读取 Excel 注释吗?

python pandas - 用字符串替换数字

python - 从具有重复自定义索引的 Dataframe 中删除行并保留具有列最大值的行