我已将XML数据转换为数据帧。现在,我希望将数据转换为理想的df输出。请帮帮我
我的当前输出:
data name
0 Aruba Country or Area
1 Population, total Item
2 1960 Year
3 54211 Value
4 Aruba Country or Area
5 Population, total Item
6 1961 Year
7 55438 Value
8 Aruba Country or Area
9 Population, total Item
10 1962 Year
11 56225 Value
12 Aruba Country or Area
13 Population, total Item
14 1963 Year
15 56695 Value
16 Aruba Country or Area
17 Population, total Item
18 1964 Year
19 57032 Value
我粘贴的最后一行直接什么,我用轴
xml_df = xml_df.pivot(index='data', columns='name')
脱色输出:
Country or Area Year Item Value
Aruba 1960 Population, total 54211
Aruba 1961 Population, total 55348
等等
枢轴的另一种方式:
输出:
IIUC,您可以在
name
和cumcount
上尝试groupby
,然后是unstack
:详情: ^{}
这将按名称和} 将一个新列
Numbers each item in each group from 0 to the length of that group - 1
分组,并使用^{k
分配给数据帧。然后使用set_index()
wee将名称和k
列设置为索引,以便获得:使用这些数据,我们使用^{} ,这有助于“透视一级(必要的层次结构)索引标签,返回一个具有新级别列标签的数据帧,该列标签的最内层由透视索引标签组成”,因此这会根据我们的需要将索引的最后一级(默认情况下)转换为列
相关问题 更多 >
编程相关推荐