pandas 熊猫删除所有不是“日期时间”类型的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39604094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:03:25  来源:igfitidea点击:

Pandas delete all rows that are not a 'datetime' type

pythonpandas

提问by Lukasz

I've got a large file with login information for a list of users. The problem is that the file includes other information in the Datecolumn. I would like to remove all rows that are not of type datetimein the Datecolumn. My data resembles

我有一个包含用户列表登录信息的大文件。问题是该文件在Date列中包含其他信息。我想删除该类型不是所有行datetimeDate列。我的数据类似于

df=
Name      Date
name_1    | 2012-07-12 22:20:00
name_1    | 2012-07-16 22:19:00
name_1    | 2013-12-16 17:50:00
name_1    |                4345 # type = 'int'
                                # type = 'float'
name_2    | 2010-01-11 19:54:00 
name_2    | 2010-02-06 12:10:00
...
name_2    | 2012-07-18 22:12:00
name_2    |                4521
...
name_5423 | 2013-11-23 10:21:00
...
name_5423 |                7532

I've tried modifying the solution to

我已经尝试将解决方案修改为

finding non-numeric rows in dataframe in pandas?

在Pandas的数据框中查找非数字行?

Remove rows where column value type is string Pandas

删除列值类型为字符串 Pandas 的行

and How-should-I-delete-rows-from-a-DataFrame-in-Python-Pandas

以及我应该如何从 Python-Pandas 中的 DataFrame 中删除行

to fit my needs.

以满足我的需要。

The problem is that whenever I attempt the change I either get an error or the entire dataframe gets deleted

问题是,每当我尝试更改时,我都会收到错误或整个数据框被删除

回答by piRSquared

Use pd.to_datetimewith parameter errors='coerce'to make non-dates into NaTnull values. Then you can drop those rows

使用pd.to_datetimewith 参数errors='coerce'使非日期变为NaT空值。然后你可以删除这些行

df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
df = df.dropna(subset=['Date'])

df

enter image description here

在此处输入图片说明