提取 Pandas Python 中的日期时间数据框的行。

huangapple go评论71阅读模式
英文:

extract rows of a datetime dataframe pandas python

问题

以下是您要翻译的内容:

我有以下的日期时间数据框,我已将日期时间列设置为我的索引

Datetime                 ratio                     dif

2022-06-09 12:33:00   -0.3861241598107547   -299.50183804712964

2022-06-09 12:34:00   -0.360130489922861    -274.88184087028105

2022-06-09 12:35:00   -0.22108950904852795  -166.02672464097395

2022-06-09 12:36:00   -0.18316426991752388  -135.2928226604197

2022-06-09 12:37:00   -0.09932437001820388  -72.14644129886278

2022-06-09 12:38:00   -0.0820362738010348   -58.58522049972339

2022-06-09 12:39:00   -0.04310125282586597  -30.25449340858836

2022-06-09 12:40:00   -0.48650055935157194  -335.5773828284086

2022-06-09 12:41:00   -0.040251923613339506  -27.620467464237436

我想选择数据框的特定行以创建一个新的数据框
我尝试了以下操作

df2 = frames.loc[frames["Datetime"].between('2022-06-09 12:34:00','2022-06-09 12:38:00 ')]

但输出是

File "C:\Users\spain\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3631, in get_loc
    raise KeyError(key) from err

KeyError: 'Datetime'
有人可以帮我吗?
英文:

i have this following Datetime dataframe that i have set the datetime column as my index

Datetime ratio dif

2022-06-09 12:33:00 -0.3861241598107547 -299.50183804712964

2022-06-09 12:34:00 -0.360130489922861 -274.88184087028105

2022-06-09 12:35:00 -0.22108950904852795 -166.02672464097395

2022-06-09 12:36:00 -0.18316426991752388 -135.2928226604197

2022-06-09 12:37:00 -0.09932437001820388 -72.14644129886278

2022-06-09 12:38:00 -0.0820362738010348 -58.58522049972339

2022-06-09 12:39:00 -0.04310125282586597 -30.25449340858836

2022-06-09 12:40:00 -0.48650055935157194 -335.5773828284086

2022-06-09 12:41:00 -0.040251923613339506 -27.620467464237436

i want to select specific rows of the dataframe in order to make a new one
i tried the following

df2 = frames.loc[frames["Datetime"].between('2022-06-09 12:34:00','2022-06-09 12:38:00' )]

and the ouput is

File "C:\Users\spain\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3631, in get_loc
raise KeyError(key) from err

KeyError: 'Datetime'
could anybody help me ?

答案1

得分: 0

# 将列 Datetime 设置为索引(如果尚未设置)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df = df.sort_values(by='Datetime').set_index('Datetime')

然后你可以过滤你的数据框:

print(df['2023-03-03 12:34:00':'2023-03-03 12:38:00'])

打印结果:

                        ratio         dif
Datetime                                 
2023-03-03 12:34:00 -0.360130 -274.881841
2023-03-03 12:35:00 -0.221090 -166.026725
2023-03-03 12:36:00 -0.183164 -135.292823
2023-03-03 12:37:00 -0.099324  -72.146441
2023-03-03 12:38:00 -0.082036  -58.585220

<details>
<summary>英文:</summary>

Try:

set column Datetime as index (if not already)

df['Datetime'] = pd.to_datetime(df['Datetime'])
df = df.sort_values(by='Datetime').set_index('Datetime')


and then you can filter your dataframe:

print(df['2023-03-03 12:34:00':'2023-03-03 12:38:00'])


Prints:

```none
                        ratio         dif
Datetime                                 
2023-03-03 12:34:00 -0.360130 -274.881841
2023-03-03 12:35:00 -0.221090 -166.026725
2023-03-03 12:36:00 -0.183164 -135.292823
2023-03-03 12:37:00 -0.099324  -72.146441
2023-03-03 12:38:00 -0.082036  -58.585220

huangapple
  • 本文由 发表于 2023年3月3日 23:43:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定