简体   繁体   中英

Read unstructured data pandas

I have data set in excel which has not an excellent table format. Here is the sample:

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

I want to read this data in Python and put it into pandas data frame like:

Country    Year    Male  Female
AFG         2010   182    120
...

Is there any way to this in Python/Pandas without manipulating the original data set?

You can fine the sample data set here:

https://expirebox.com/download/173bc0880dd9da56ccff2796aa1274ed.html

Thanks

A solution - provided by pandas native excel reader options.

found the technique here: reading excel sheet as multiindex dataframe through pd.read_excel()

df = pd.read_excel('Sample.xlsx',header=[0,1],index_col=[0,1])

which gives:

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

and to finish out to the desired layout use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM