Python 使用pd.concat堆叠数据帧-包括列名
我有一个如下所示的数据帧:Python 使用pd.concat堆叠数据帧-包括列名,python,pandas,dataframe,pivot,Python,Pandas,Dataframe,Pivot,我有一个如下所示的数据帧: import pandas as pd df = pd.read_csv("Stream_Deaths.csv", index_col="Date", parse_dates=["Date"]) df = df.T df = df.loc["3/11/2020":] print(df.head()) #Output Date Europe Americas Asia Oceania Africa US
import pandas as pd
df = pd.read_csv("Stream_Deaths.csv", index_col="Date", parse_dates=["Date"])
df = df.T
df = df.loc["3/11/2020":]
print(df.head())
#Output
Date Europe Americas Asia Oceania Africa US \#etc
3/11/2020 120.571429 4.000000 69.285714 0.142857 0.285714 3.571429
3/12/2020 142.000000 4.714286 76.142857 0.142857 0.428571 4.142857
3/13/2020 188.285714 5.714286 83.571429 0.142857 0.857143 5.000000
3/14/2020 222.857143 6.857143 93.857143 0.142857 1.000000 5.857143
3/15/2020 272.285714 8.285714 100.000000 0.000000 1.000000 7.428571
然后我使用pd.concat
来堆叠数据帧:
df_stacked = pd.concat([df["Europe"], df["Americas"], df["Asia"], df["Oceania"], df["Africa"],
df["US"], df["Brazil"], df["Mexico"], df["UK"], df["Rest of Europe"],
df["Rest of Latam"],df["India"],df["Rest of Asia"], df["Rest of North America"],
df["Canada"],df["Bermuda"],df["Greenland"]],
axis=0, ignore_index=False)
print(df_stacked.head())
#Output
3/11/2020 120.571429
3/12/2020 142.000000
3/13/2020 188.285714
3/14/2020 222.857143
3/15/2020 272.285714
如何将列名作为数据帧的索引包括在内?我尝试过在pd.concat
中使用名称
选项,但迄今为止没有任何运气。
我的期望输出:
Europe 3/11/2020 120.571429
Europe 3/12/2020 142.000000
Europe 3/13/2020 188.285714
Europe 3/14/2020 222.857143
Europe 3/15/2020 272.285714
...
Americas 3/11/2020 120.571429
Americas 3/12/2020 142.000000
Americas 3/13/2020 188.285714
Americas 3/14/2020 222.857143
Americas 3/15/2020 272.285714
#etc
IIUC,试试:
df.melt('Date')
输出:
Date variable value
0 3/11/2020 Europe 120.571429
1 3/12/2020 Europe 142.000000
2 3/13/2020 Europe 188.285714
3 3/14/2020 Europe 222.857143
4 3/15/2020 Europe 272.285714
5 3/11/2020 Americas 4.000000
6 3/12/2020 Americas 4.714286
7 3/13/2020 Americas 5.714286
8 3/14/2020 Americas 6.857143
9 3/15/2020 Americas 8.285714
10 3/11/2020 Asia 69.285714
11 3/12/2020 Asia 76.142857
12 3/13/2020 Asia 83.571429
13 3/14/2020 Asia 93.857143
14 3/15/2020 Asia 100.000000
15 3/11/2020 Oceania 0.142857
16 3/12/2020 Oceania 0.142857
17 3/13/2020 Oceania 0.142857
18 3/14/2020 Oceania 0.142857
19 3/15/2020 Oceania 0.000000
20 3/11/2020 Africa 0.285714
21 3/12/2020 Africa 0.428571
22 3/13/2020 Africa 0.857143
23 3/14/2020 Africa 1.000000
24 3/15/2020 Africa 1.000000
25 3/11/2020 US 3.571429
26 3/12/2020 US 4.142857
27 3/13/2020 US 5.000000
28 3/14/2020 US 5.857143
29 3/15/2020 US 7.428571
不要压缩,而是取消堆栈:
df.设置索引('Date')。重命名轴('Countries',axis=1)。取消堆栈()。重置索引(name='yourcol')