0

Okay, all I would like to do is produce a nice bar chart of the populations of the Countries of the World.

pop_list = pd.read_table('country.dat', names=cols)

pop_list.dtypes
# Rank                 int64
# Country                  object
# Population           object

print(type(pop_list['Population']))
pd.Series.hist(pop_list['Population'])

gives an error: TypeError: unorderable types: str() < float()

pop_list.plot.hist(['Population'],alpha=0.5)
plt.show()

makes a rubbish plot, presumably of 'Rank', and finally

pop_list['Population'].plot().hist()

gives: TypeError: Empty 'DataFrame': no numeric data to plot

npross
  • 1,756
  • 6
  • 19
  • 38
  • What is ``'country.dat'``? Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – jakevdp Apr 17 '17 at 16:15
  • P.S. The full code is on my GitHub:: https://github.com/d80b2t/python/blob/master/wealth_and_population/population_barchart_forStackOverflow.ipynb – npross Apr 17 '17 at 16:17
  • 0 World World World 7,432,663,275 7,349,472,099 +1.1% 1 China Asia Eastern Asia 1,382,323,332 1,376,048,943 +0.5% 2 India Asia Southern Asia 1,326,801,576 1,311,050,527 +1.2% 3 United States Americas Northern America 324,118,787 321,773,631 +0.7% 4 Indonesia Asia South-Eastern Asia 260,581,100 257,563,815 +1.2% – npross Apr 17 '17 at 16:17
  • It looks like ``read_table`` is getting tripped-up on the numbers with commas in them, and leaving them as strings. When you try to plot a histogram of strings, you get the error saying there is no numeric data. [This answer](http://stackoverflow.com/questions/22137723/convert-number-strings-with-commas-in-pandas-dataframe-to-float) might be helpful. – jakevdp Apr 17 '17 at 16:21

1 Answers1

1

After data answer is different - need parameter thousands:

cols = ['a', 'b', 'c', 'd']
pop_list = pd.read_table('country.dat', names=cols, thousands=',')
print (pop_list)
                                         a           b           c      d
0                        World World World  7432663275  7349472099  +1.1%
1                  China Asia Eastern Asia  1382323332  1376048943  +0.5%
2                 India Asia Southern Asia  1326801576  1311050527  +1.2%
3  United States Americas Northern America   324118787   321773631  +0.7%
4        Indonesia Asia South-Eastern Asia   260581100   257563815  +1.2%

print (pop_list.dtypes)
a    object
b     int64
c     int64
d    object
dtype: object
Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252