简体   繁体   English

如何在 Pandas 中取消旋转列并动态命名月份?

[英]How to unpivot columns in pandas and name the months dynamically?

I need some help in converting the columns to rows and name the columns dynamically by month.我需要一些帮助来将列转换为行并按月动态命名列。

Please see the attached raw_data image请参阅随附的raw_data 图像

For example: Let say the current month is March, the Demand00 month name would be March, Demand01 would be April and so on.例如:假设当前月份是三月,Demand00 月份名称将是三月,Demand01 将是四月等等。 If I run this same code in April, Demand00 column name should be named as April and Demand01 should be named as May and so on.如果我在 4 月运行相同的代码,Demand00 列名称应命名为 April,Demand01 应命名为 May,依此类推。

This is my first post, I hope I gave you the relevant information to seek help, if i had missed anything please let me know.这是我的第一篇文章,我希望我给了你相关的信息来寻求帮助,如果我遗漏了什么,请告诉我。

Thank you in advance for your help.预先感谢您的帮助。

This should do:这应该做:

import datetime as dt
import calendar

this_month=dt.datetime.now().month
demand_columns=[i for i in df.columns if 'Demand' in i]
month_list=[calendar.month_name[this_month+i] for i in range(len(demand_columns))]
dic_month={col:month for col,month in zip(demand_columns,month_list)}
df.rename(columns=dic_month)

Line 1 extracts current month第 1 行提取当前月份

Line 2 extracts every column that has 'Demand' in its name第 2 行提取名称中包含“需求”的每一列

Line 3 creates a list of Months from today's month to your last column's month and turns month as an integer into its month's name第 3 行创建从今天的月份到最后一列月份的月份列表,并将月份作为整数转换为其月份名称

Line 4 maps columns to months第 4 行将列映射到月份

Line 5 renames your columns.第 5 行重命名您的列。

Edit: added answer with month name instead of number编辑:添加了带有月份名称而不是数字的答案

pd.DateOffset is nice to compute next months, and stack can convert columns to rows. pd.DateOffset很适合计算pd.DateOffsetstack可以将列转换为行。

So code could be:所以代码可能是:

# compute month names:
cols = [x for x in df.columns if x.startswith('Demand')]
today = pd.Timestamp.now()
months=[(today+pd.DateOffset(months=i)).month_name()
        for i in range(len(cols))]

# rename columns and stack:
df2 = pd.DataFrame(df.rename(columns=dict(zip(cols, months)))
                   .set_index('StockCode').stack()).reset_index()
df2.columns = ['StockCode', 'Month', 'Value']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM