[英]How to melt the pd.DataFrame to organize the data? (toy example included)
data_df
in the toy example provided below to the desired_df
.data_df
在下面提供的玩具例子desired_df
。import pandas as pd
data_df = pd.DataFrame(data = [['FR','Aug',100], ['FR','Sep',170], ['FR','Oct',250],
['KR','Aug',9], ['KR','Sep',12],['KR','Oct',19],
['US','Aug',360], ['US','Sep',500], ['US','Oct',700]],
columns = ['country','time','covid19'])
data_df
>>> country time covid19
0 FR Aug 100
1 FR Sep 170
2 FR Oct 250
3 KR Aug 9
4 KR Sep 12
5 KR Oct 19
6 US Aug 360
7 US Sep 500
8 US Oct 700
desired_df
is as follows, country names at columns
, time at index
, and number of Covid 19 patients in the dataframe as values
.desired_df
如下, columns
国家名称、 index
时间以及数据框中 Covid 19 患者的数量作为values
。desired_df
>>> FR KR US
Aug 100 9 360
Sep 170 12 500
Oct 250 19 700
pd.melt
would help, but it does not create index and columns as I wanted.pd.melt
会有所帮助,但它不会按照我的pd.melt
创建索引和列。Try pivot
:尝试
pivot
:
data = data_df.pivot(index = 'time', columns = 'country')
print(data)
Which gives:这使:
country FR KR US
time
Aug 100 9 360
Oct 250 19 700
Sep 170 12 500
The indices are in alphabetical order.索引按字母顺序排列。 Reorder them as you like.
根据需要重新排列它们。 For ordering them calendrically, I'd suggest Brad Solomon's answer to Sort a pandas's dataframe series by month name?
为了按日历排序它们,我建议 Brad Solomon 的回答按月份名称对熊猫的数据框系列进行排序? , which uses the
pd.Categorical
. ,它使用
pd.Categorical
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.