堆叠 header,而不是两列
Stack the header, not the two columns
尝试堆叠我的 table,但如果我没有 "country column",效果很好。我如何保持第一个列“未堆叠,只堆叠日期的行。下图演示了我想要的。
左图是table的样子,右图是我想要的格式。所以问题是我如何在行之后堆叠,通常你在列级别之后堆叠。
一切顺利,
你需要:
cols = ['GEO','INDIC',1990,1991,1992]
df = pd.DataFrame({'GEO':['Austria']*3, 'INDIC':['dis','fin1','fin2'],
1990:[2,42,17],1991:[3,44,18],1992:[2,44,17]}, columns=cols)
print (df)
GEO INDIC 1990 1991 1992
0 Austria dis 2 3 2
1 Austria fin1 42 44 44
2 Austria fin2 17 18 17
1.
通过 set_index
of all columns for not reshape and then add stack
, rename_axis
and reset_index
创建索引用于新列名:
df1 = df.set_index(['GEO','INDIC'])
.stack()
.rename_axis(['GEO','INDIC', 'year'])
.reset_index(name='quantity')
print (df1)
GEO INDIC year quantity
0 Austria dis 1990 2
1 Austria dis 1991 3
2 Austria dis 1992 2
3 Austria fin1 1990 42
4 Austria fin1 1991 44
5 Austria fin1 1992 44
6 Austria fin2 1990 17
7 Austria fin2 1991 18
8 Austria fin2 1992 17
2.
通过 melt
重塑,列的排序方式不同:
df1 = df.melt(id_vars=['GEO','INDIC'], var_name='year', value_name='quantity')
print (df1)
GEO INDIC year quantity
0 Austria dis 1990 2
1 Austria fin1 1990 42
2 Austria fin2 1990 17
3 Austria dis 1991 3
4 Austria fin1 1991 44
5 Austria fin2 1991 18
6 Austria dis 1992 2
7 Austria fin1 1992 44
8 Austria fin2 1992 17
尝试堆叠我的 table,但如果我没有 "country column",效果很好。我如何保持第一个列“未堆叠,只堆叠日期的行。下图演示了我想要的。
左图是table的样子,右图是我想要的格式。所以问题是我如何在行之后堆叠,通常你在列级别之后堆叠。
一切顺利,
你需要:
cols = ['GEO','INDIC',1990,1991,1992]
df = pd.DataFrame({'GEO':['Austria']*3, 'INDIC':['dis','fin1','fin2'],
1990:[2,42,17],1991:[3,44,18],1992:[2,44,17]}, columns=cols)
print (df)
GEO INDIC 1990 1991 1992
0 Austria dis 2 3 2
1 Austria fin1 42 44 44
2 Austria fin2 17 18 17
1.
通过 set_index
of all columns for not reshape and then add stack
, rename_axis
and reset_index
创建索引用于新列名:
df1 = df.set_index(['GEO','INDIC'])
.stack()
.rename_axis(['GEO','INDIC', 'year'])
.reset_index(name='quantity')
print (df1)
GEO INDIC year quantity
0 Austria dis 1990 2
1 Austria dis 1991 3
2 Austria dis 1992 2
3 Austria fin1 1990 42
4 Austria fin1 1991 44
5 Austria fin1 1992 44
6 Austria fin2 1990 17
7 Austria fin2 1991 18
8 Austria fin2 1992 17
2.
通过 melt
重塑,列的排序方式不同:
df1 = df.melt(id_vars=['GEO','INDIC'], var_name='year', value_name='quantity')
print (df1)
GEO INDIC year quantity
0 Austria dis 1990 2
1 Austria fin1 1990 42
2 Austria fin2 1990 17
3 Austria dis 1991 3
4 Austria fin1 1991 44
5 Austria fin2 1991 18
6 Austria dis 1992 2
7 Austria fin1 1992 44
8 Austria fin2 1992 17