按列NAME dtype选择列

时间:2017-10-13 11:00:10

标签: 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)

如何才能回到第2和第4列(dtype' date time.datetime')?列内容的类型完全相同,因此select_dtypes没有帮助。

1 个答案:

答案 0 :(得分:2)

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