如何更改重采样列的名称?
我有一个数据框,其中包含纳斯达克股票指数每分钟的价格波动。 在交易中,考虑不同时间单位的数据非常重要(了解短期、中期和长期趋势...)
因此我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用:
输出:
您忘记将结果重新分配给数据框:
输出:
You can use:
Output:
You forgot to reassign the result to your dataframe:
Output:
相信您的问题是您在重命名中缺少选项
inplace=true
。默认情况下它是 false,因此它会生成 DataFrame 的副本,而不是编辑现有的 DataFrame。将其设置为 true 将编辑现有 DataFrame df_1m_5m的 df5m 输出:
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 DataFramedf5m
Output of df_1m_5m:
同意斯蒂芬和科拉林的观点。你也可以尝试这个:
Agree with Stephan and Corralien. You can also try this: