How to create Histograms in Panda Python Using Specific Rows and Columns in Data Frame

百般思念 提交于 2019-12-11 05:29:20

问题


I have the following data frame in the picture, i want to take a Plot a histogram to show the distribution of all countries in the world for any given year (e.g. 2010).

Following is my code table generates after the following code of cleaning:

dataSheet = pd.read_excel("http://api.worldbank.org/v2/en/indicator/EN.ATM.CO2E.PC?downloadformat=excel",sheetname="Data")
dataSheet = dataSheet.transpose()
dataSheet = dataSheet.drop(dataSheet.columns[[0,1]], axis=1) ;
dataSheet = dataSheet.drop(['World Development Indicators', 'Unnamed: 2','Unnamed: 3'])


回答1:


In order to plot a histogram of all countries for any given year (e.g. 2010), I would do the following. After your code:

dataSheet = pd.read_excel("http://api.worldbank.org/v2/en/indicator/EN.ATM.CO2E.PC?    downloadformat=excel",sheetname="Data")
dataSheet = dataSheet.transpose()
dataSheet = dataSheet.drop(dataSheet.columns[[0,1]], axis=1)
dataSheet = dataSheet.drop(['World Development Indicators', 'Unnamed: 2','Unnamed: 3'])

I would reorganise the column names, by assigning the actual country names as column names:

dataSheet.columns = dataSheet.iloc[1] # here I'm assigning the column names
dataSheet = dataSheet.reindex(dataSheet.index.drop('Data Source')) # here I'm re-indexing and getting rid of the duplicate row

Then I would transpose the data frame again (to be safe I'm assigning it to a new variable):

df = dataSheet.transpose()

And then I'd do the same as I did before with assigning new column names, so we get a decent data frame (although still not optimal) with country names as index.

df.columns = df.iloc[0]
df = df.reindex(df.index.drop('Country Name'))

Now you can finally plot the histogram for e.g. year 2010:

import matplotlib.pyplot as plt
df[2010].plot(kind='bar', figsize=[30,10])



来源:https://stackoverflow.com/questions/40710747/how-to-create-histograms-in-panda-python-using-specific-rows-and-columns-in-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!