如何更改重采样列的名称?

发布于 2025-01-13 07:15:22 字数 865 浏览 0 评论 0原文

我有一个数据框,其中包含纳斯达克股票指数每分钟的价格波动。 在交易中,考虑不同时间单位的数据非常重要(了解短期、中期和长期趋势...)

因此我使用Pandas的resample()方法在5分钟内获得带有价格的数据框除了原来的 1 分钟之外:

df1m = pd.DataFrame({
'Time' : ['2022-01-11 09:30:00', '2022-01-11 09:31:00', '2022-01-11 09:32:00', '2022-01-11 09:33:00', '2022-01-11 09:34:00', '2022-01-11 09:35:00', '2022-01-11 09:36:00' , '2022-01-11 09:37:00' , '2022-01-11 09:38:00' ,
       '2022-01-11 09:39:00', '2022-01-11 09:40:00'],  
'Price' : [1,2,3,4,5,6,7,8,9,10,11]})
df1m['Time'] = pd.to_datetime(df1m['Time'])
df1m.set_index(['Time'], inplace =True)

df5m = df1m.resample('5min').first() 

我将列名称重命名为 5min :

df5m.rename(columns={'Price' : 'Price5'})

不幸的是,当两个数据帧(1 和 5 分钟)放在一起时,不再考虑列名称的更改:

df_1m_5m = pd.concat([df1m, df5m], axis=1)

如何明确重命名为创建的列5分钟数据并避免不同数据使用两次相同的列名?

I have a dataframe with the price fluctuations of the Nasdaq stock index every minute.
In trading it is important to take into account data on different time units (to know the short term, medium and long term trends...)

So I used the resample() method of Pandas to get a dataframe with the price in 5 minutes in addition to the original 1 minute:

df1m = pd.DataFrame({
'Time' : ['2022-01-11 09:30:00', '2022-01-11 09:31:00', '2022-01-11 09:32:00', '2022-01-11 09:33:00', '2022-01-11 09:34:00', '2022-01-11 09:35:00', '2022-01-11 09:36:00' , '2022-01-11 09:37:00' , '2022-01-11 09:38:00' ,
       '2022-01-11 09:39:00', '2022-01-11 09:40:00'],  
'Price' : [1,2,3,4,5,6,7,8,9,10,11]})
df1m['Time'] = pd.to_datetime(df1m['Time'])
df1m.set_index(['Time'], inplace =True)

df5m = df1m.resample('5min').first() 

I renamed the column names to 5min :

df5m.rename(columns={'Price' : 'Price5'})

Unfortunately the change of column names is no longer taken into account when the two dataframes (1 and 5 min) are put together:

df_1m_5m = pd.concat([df1m, df5m], axis=1)

How to rename definitively the columns created for the 5min data and avoid having twice the same column name for different data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花间憩 2025-01-20 07:15:22

您可以使用:

df5m = df1m.resample('5min').first().add_suffix('5')
df_1m_5m = pd.concat([df1m, df5m], axis=1)

输出:

>>> df_1m_5m
                     Price  Price5
Time                              
2022-01-11 09:30:00      1     1.0
2022-01-11 09:31:00      2     NaN
2022-01-11 09:32:00      3     NaN
2022-01-11 09:33:00      4     NaN
2022-01-11 09:34:00      5     NaN
2022-01-11 09:35:00      6     6.0
2022-01-11 09:36:00      7     NaN
2022-01-11 09:37:00      8     NaN
2022-01-11 09:38:00      9     NaN
2022-01-11 09:39:00     10     NaN
2022-01-11 09:40:00     11    11.0

您忘记将结果重新分配给数据框:

df5m = df5m.rename(columns={'Price' : 'Price5'})

# OR

df5m.rename(columns={'Price' : 'Price5'}, inplace=True)

输出:

>>> df5m
                     Price5
Time                       
2022-01-11 09:30:00       1
2022-01-11 09:35:00       6
2022-01-11 09:40:00      11

You can use:

df5m = df1m.resample('5min').first().add_suffix('5')
df_1m_5m = pd.concat([df1m, df5m], axis=1)

Output:

>>> df_1m_5m
                     Price  Price5
Time                              
2022-01-11 09:30:00      1     1.0
2022-01-11 09:31:00      2     NaN
2022-01-11 09:32:00      3     NaN
2022-01-11 09:33:00      4     NaN
2022-01-11 09:34:00      5     NaN
2022-01-11 09:35:00      6     6.0
2022-01-11 09:36:00      7     NaN
2022-01-11 09:37:00      8     NaN
2022-01-11 09:38:00      9     NaN
2022-01-11 09:39:00     10     NaN
2022-01-11 09:40:00     11    11.0

You forgot to reassign the result to your dataframe:

df5m = df5m.rename(columns={'Price' : 'Price5'})

# OR

df5m.rename(columns={'Price' : 'Price5'}, inplace=True)

Output:

>>> df5m
                     Price5
Time                       
2022-01-11 09:30:00       1
2022-01-11 09:35:00       6
2022-01-11 09:40:00      11
九公里浅绿 2025-01-20 07:15:22

相信您的问题是您在重命名中缺少选项 inplace=true 。默认情况下它是 false,因此它会生成 DataFrame 的副本,而不是编辑现有的 DataFrame。将其设置为 true 将编辑现有 DataFrame df_1m_5m

df5m.rename(columns={'Price' : 'Price5'},inplace=True)

的 df5m 输出:

                     Price  Price5
Time
2022-01-11 09:30:00      1     1.0
2022-01-11 09:31:00      2     NaN
2022-01-11 09:32:00      3     NaN
2022-01-11 09:33:00      4     NaN
2022-01-11 09:34:00      5     NaN
2022-01-11 09:35:00      6     6.0
2022-01-11 09:36:00      7     NaN
2022-01-11 09:37:00      8     NaN
2022-01-11 09:38:00      9     NaN
2022-01-11 09:39:00     10     NaN
2022-01-11 09:40:00     11    11.0

Believe your issue is you are missing option inplace=true in your rename. By default it's false, so it generates a copy of the DataFrame rather than editing your existing DataFrame. Setting it to true will edit your existing DataFrame df5m

df5m.rename(columns={'Price' : 'Price5'},inplace=True)

Output of df_1m_5m:

                     Price  Price5
Time
2022-01-11 09:30:00      1     1.0
2022-01-11 09:31:00      2     NaN
2022-01-11 09:32:00      3     NaN
2022-01-11 09:33:00      4     NaN
2022-01-11 09:34:00      5     NaN
2022-01-11 09:35:00      6     6.0
2022-01-11 09:36:00      7     NaN
2022-01-11 09:37:00      8     NaN
2022-01-11 09:38:00      9     NaN
2022-01-11 09:39:00     10     NaN
2022-01-11 09:40:00     11    11.0
多情出卖 2025-01-20 07:15:22

同意斯蒂芬和科拉林的观点。你也可以尝试这个:

df1m['Price5'] = df1m.resample('5T').first()

Agree with Stephan and Corralien. You can also try this:

df1m['Price5'] = df1m.resample('5T').first()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文