假设你有一个数据框
month 1 2 3
year
2019 a b c
2020 d e f
我想要的是一个转换后的数据框,其中行名(年)和列名(月)作为索引:
data
2019-01-01 a
2019-02-01 b
2019-03-01 c
2020-01-01 d
2020-02-01 e
2020-03-01 f
在 Pandas 中有没有优雅的方法来做到这一点?
构建 dfs 的最小示例
# this builds the dataframe
import numpy as np
import pandas as pd
from datetime import date
df = pd.DataFrame(np.array([['a', 'b', 'c'], ['d', 'e', 'f']]), columns = [1, 2, 3], index = [2019, 2020])
df.columns.name = "month"
df.index.name = "year"
所需的数据框:
# this builds the desire dataframe
desired_index = [date(2019,1,1), date(2019,2,1), date(2019,3,1), date(2020,1,1), date(2020,2,1), date(2020,3,1)]
desired_df = pd.DataFrame(np.array(['a', 'b', 'c', 'd', 'e', 'f']), columns = ['data'], index = desired_index)