15

I have dataset which look like this

import pandas as pd

data = {'Name of Countries': ['BANGLADESH', 'SRILANKA', 'UK', 'USA'], '2001': [431312, 112813, 405472, 329147], '2002': [435867, 108008, 387846, 348182], '2003': [454611, 109098, 430917, 410803], '2004': [477446, 128711, 555907, 526120], '2005': [456371, 136400, 651803, 611165], '2006': [484401, 154813, 734240, 696739], '2007': [480240, 204084, 796191, 799062], '2008': [541884, 218805, 776530, 804933], '2009': [468899, 239995, 769251, 827140], '2010': [431962, 266515, 759494, 931292]}
df = pd.DataFrame(data)

  Name of Countries    2001    2002    2003    2004    2005    2006    2007    2008    2009    2010
0        BANGLADESH  431312  435867  454611  477446  456371  484401  480240  541884  468899  431962
1          SRILANKA  112813  108008  109098  128711  136400  154813  204084  218805  239995  266515
2                UK  405472  387846  430917  555907  651803  734240  796191  776530  769251  759494
3               USA  329147  348182  410803  526120  611165  696739  799062  804933  827140  931292

I am trying to plot row with y axis as the values and x axis being the year, eg. I tried for USA

t=df[df['Name of Countries']=='USA']
x=pd.DataFrame([t.iloc[0].index,t.iloc[0].values]).T
x.iloc[1:].plot()
plt.show() 

Which is completely ugly looking code, . What I get is enter image description here

I want -USA at legend and X axis as name of columns [2001,2002...2010], and can it be done in a better way, without going through individual row like I am doing.

0

2 Answers 2

19

You need to specify that Name of Countries is your index when you load the df. Also, it seems to me that for your purposes using countries as columns and years as rows would be a more sensible choice.

df = pd.read_csv(yourcsv, index_col='Name of Countries')  # set column as index
df = df.T  # Transpose df, now countries are your columns and years your rows

# display(df)
Name of Countries  BANGLADESH  SRILANKA      UK     USA
2001                   431312    112813  405472  329147
2002                   435867    108008  387846  348182
2003                   454611    109098  430917  410803
2004                   477446    128711  555907  526120
2005                   456371    136400  651803  611165
2006                   484401    154813  734240  696739
2007                   480240    204084  796191  799062
2008                   541884    218805  776530  804933
2009                   468899    239995  769251  827140
2010                   431962    266515  759494  931292

Once you load the df in that way everything is super easy:

df.USA.plot(legend=True) #plot usa column
plt.show()

enter image description here

Plot a single figure

ax = df.plot()

enter image description here

df.plot(subplots=True, figsize=(10, 8), layout=(2, 2))

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Just a remark, this crashes when run from my emacs codeblock. plt.plot( df['something'] ) and plt.show() works fine from emacs.
3

If you don't want to transpose your dataframe, you can use df.iterrows():

df = df.set_index('Name of Countries')

for index, row in df.iterrows():
    plt.plot(row, label=index)
plt.legend()
plt.show()

enter image description here

  • Put plt.legend() and plt.show() inside the loop for individual plots.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.