pandas - 从时间索引数据框中删除一行

标签 pandas time-series delete-row dataframe

我试图通过简单地传递日期和时间来删除 Pandas 数据框中的一行。

数据框具有以下结构:

Date_Time             Price1   Price2    Price3                       
2012-01-01 00:00:00    63.05    41.40    68.14
2012-01-01 01:00:00    68.20    42.44    59.64
2012-01-01 02:00:00    61.68    43.18    49.81

我一直在尝试 df = df.drop('2012-01-01 01:00:00')
但我不断收到以下错误消息:
exceptions.ValueError: labels [2012-01-01 01:00:00] not contained in axis

任何有关删除行或仅删除值的帮助将不胜感激。

:-)

最佳答案

看起来您必须实际使用时间戳而不是字符串:

In [11]: df1
Out[11]:
                     Price1  Price2  Price3
Date_Time
2012-01-01 00:00:00   63.05   41.40   68.14
2012-01-01 01:00:00   68.20   42.44   59.64
2012-01-01 02:00:00   61.68   43.18   49.81

In [12]: df1.drop(pd.Timestamp('2012-01-01 01:00:00'))
Out[12]:
                     Price1  Price2  Price3
Date_Time
2012-01-01 00:00:00   63.05   41.40   68.14
2012-01-01 02:00:00   61.68   43.18   49.81

假设 DateTime 是索引,如果不使用
df1 = df.set_index('Date_Time')

关于pandas - 从时间索引数据框中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16613187/

相关文章:

matlab - 组合不同频率的数据矩阵

ios - 删除动画不显示

swift - 无效更新 : invalid number of rows in section 0, 更新后现有节中包含的行数

python - Pandas 将科学计数法中的 float 转换为字符串

python - 使用列表理解时数据框被覆盖

Python:在 DataFrame 中,如何循环遍历一列的所有字符串并检查它们是否出现在另一列中并对它们进行计数?

python - 无法导入 Pandas 和 numpy

machine-learning - 使用 PyTorch 向 LSTM 提供多个输入以进行时间序列预测

c# - EntityFramework 6.0 每行生成一个 DELETE

sql - 如何将列添加到 generate_series 查询