我将列名作为月份(字符串),我需要将其名称更改为日期格式
我的数据:
PC GEO 1月2月3日
A Ind 1 1 1
预期输出
PC GEO 2019-01-01 2019-02-01 2019-03-01
A Ind 1 1 1
答案 0 :(得分:1)
使用:
df = df.set_index(['PC','GEO'])
df.columns = pd.to_datetime(df.columns + '2019', format='%b%Y')
print (df)
2019-01-01 2019-02-01 2019-03-01
PC GEO
A Ind 1 1 1
然后您可以通过stack
重塑:
df = df.rename_axis('date', axis=1).stack().reset_index(name='val')
print (df)
PC GEO date val
0 A Ind 2019-01-01 1
1 A Ind 2019-02-01 1
2 A Ind 2019-03-01 1