使用 pandas 获取所有具有datetime类型的列? [英] Get all columns with datetime type using pandas?

查看:38
本文介绍了使用 pandas 获取所有具有datetime类型的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的DataFrame,其中的列从来都不是按顺序排列的,也不知道它们的名称.

I have a huge DataFrame where the columns aren't ever in order nor do I know their name.

如何查找所有属于日期时间类型的列?

What do I do to find all the columns which are datetime types?

大多数在线解决方案中,发布者都知道专栏的名称,所以我有点麻烦,而我却没有.在这种情况下我该怎么办?

Most of the solutions online, the poster knows the name of the column so I am having a bit trouble as I do not. What can I do in this situation?

推荐答案

您可以使用

You can use pandas.DataFrame.select_dtypes(), and include only the datetime64 type.

df.select_dtypes(include=['datetime64'])

演示

>>> df
         dts1       dts2  ints
0  2012-01-01 2004-01-01     0
1  2012-01-02 2004-01-02     1
2  2012-01-03 2004-01-03     2
..        ...        ...   ...
97 2012-04-07 2004-04-07    97
98 2012-04-08 2004-04-08    98
99 2012-04-09 2004-04-09    99

>>> df.select_dtypes(include=['datetime64'])
         dts1       dts2
0  2012-01-01 2004-01-01
1  2012-01-02 2004-01-02
2  2012-01-03 2004-01-03
..        ...        ...
97 2012-04-07 2004-04-07
98 2012-04-08 2004-04-08
99 2012-04-09 2004-04-09

这篇关于使用 pandas 获取所有具有datetime类型的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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