如何删除 Pandas 数据框中特定日期的行? [英] How can I delete rows for a particular Date in a Pandas dataframe?

查看:461
本文介绍了如何删除 Pandas 数据框中特定日期的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Date 作为索引的 Pandas DataFrame.如何删除所有日期为2000-01-06"的行?

I've got a Pandas DataFrame using Date as an index. How can I drop all rows that have the date "2000-01-06"?

示例代码:

import numpy as np
import pandas as pd

dates = pd.date_range('1/1/2000', periods=8)
df = pd.DataFrame(np.random.randn(8, 3), index=dates, columns=['A', 'B', 'C'])
df.index.name = 'Date'

示例数据帧:

                   A         B         C
Date                                    
2000-01-01 -0.501547 -0.227375  0.275931
2000-01-02  0.994459  1.266288 -0.178603
2000-01-03 -0.982746 -0.339685  0.157390
2000-01-04 -1.013987 -1.074076 -2.346117
2000-01-05 -0.101157 -0.698663  1.025318
2000-01-06 -1.697615 -0.081638  1.075712
2000-01-07  0.617587 -1.561204 -1.685774
2000-01-08  0.049115  0.579139 -1.036961

推荐答案

您可以将日期时间传递给 drop 删除该行:

You can pass a datetime to drop to drop that row:

In [12]:
df.drop(pd.to_datetime('2000-01-06'))

Out[12]:
                   A         B         C
Date                                    
2000-01-01 -0.401531 -1.076727  0.519324
2000-01-02  0.022450  0.655763 -0.592045
2000-01-03  0.579927  1.358475  0.803414
2000-01-04  0.346246 -0.252332 -1.347014
2000-01-05  0.101308  0.912279  0.020754
2000-01-07  0.869264  0.699575  0.385521
2000-01-08  0.098829 -0.237605  1.112033

这篇关于如何删除 Pandas 数据框中特定日期的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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