[英]Read unstructured data pandas
我在excel中設置了數據集,但表格格式不佳。 這是示例:
Country Male Female
2010 2011 2012 2013 2014 2010 2011 2012 2013 2014
AFG 182 134 94 87 85 120 150 95 75 92
BLZ 200 250 150 125 45 210 140 125 101 21
我想在Python中讀取此數據並將其放入pandas數據框架中,例如:
Country Year Male Female
AFG 2010 182 120
...
在不操縱原始數據集的情況下,Python / Pandas中有什么辦法嗎?
您可以在此處細化樣本數據集:
https://expirebox.com/download/173bc0880dd9da56ccff2796aa1274ed.html
謝謝
解決方案-由熊貓本機excel讀者選項提供。
在這里找到了這項技術: 通過pd.read_excel()將Excel工作表讀取為多索引數據幀
df = pd.read_excel('Sample.xlsx',header=[0,1],index_col=[0,1])
這使:
Country Male Female
1990 2000 2010 2015 2016 1990 2000 2010 2015 2016
AFG Afghanistan 127.0000 96.5000 70.0000 58.7000 56.9000 113.2000 84.7000 61.2000 50.8000 49.2000
ALB Albania 38.1000 25.5000 16.4000 13.7000 13.3000 31.0000 20.6000 13.2000 11.1000 10.7000
DZA Algeria 45.0000 36.7000 24.9000 23.2000 22.9000 37.5000 31.1000 22.0000 20.5000 20.2000
AND Andorra 8.0000 4.3000 3.2000 2.7000 2.7000 6.6000 3.7000 2.7000 2.3000 2.3000
AGO Angola 140.6000 132.7000 82.4000 62.5000 60.0000 120.9000 112.8000 68.0000 51.0000 49.0000
並完成所需的布局,請使用stack()
df.stack()
Country Female Male
AFG Afghanistan 1990 113.2000 127.0000
2000 84.7000 96.5000
2010 61.2000 70.0000
2015 50.8000 58.7000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.