python - When I try to sum csv file using pandas it does not work -


i new pandas , try sum csv file. have created csv has country , count column , use pandas sum total count of each country. not sure csv format wether converted correctly or not. code

import pandas pd df =pd.read_csv ("/users/mani/desktop/finalgeocount.csv") print df.groupby(['country']).sum() 

this raw csv file data

country count  china   1 china   1 china   1 china   2 ireland 1 china   3 moldova 1 

i need output this:

china           8 ireland         1 moldova         1 

but output showing below:

                            unnamed: 2  unnamed: 3  unnamed: 4 country                                                        albania                            nan         nan         nan algeria                            nan         nan         nan angola                             nan         nan         nan anguilla                           nan         nan         nan 

the raw data you've posted tab delimited, form of csv. when reading file helps specify delimiter:

>>> df = pd.read_csv('/users/mani/desktop/finalgeocount.csv', delimiter='\t') >>> df    country  count 0    china      1 1    china      1 2    china      1 3    china      2 4  ireland      1 5    china      3 6  moldova      1 >>> df.groupby('country').sum()          count country        china        8 ireland      1 moldova      1 

n.b. had tidy raw data little remove stray characters second blank line.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -