Python Forum

Full Version: pandas series to list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Pandas Experts,
I signed up for an online training for python and one of the problems I have is that I got a series but should make a list out of it.


def answer_six():
    statewiththemost=census_df.groupby(['CTYNAME']).CENSUS2010POP.sum().nlargest(3)
    x=statewiththemost
    return x #cal, tex, ny
answer_six()

CTYNAME
California    37253956
Texas         25145561
New York      19378102
Name: CENSUS2010POP, dtype: int64
I need to create a list with the top 3 country names as shown. not their actual population.
I tried it with x.index[0] or x.[CTYNAME] but both do not work.
I would super appreciate any help how to extract this first "column" out of my series.
df_list = df['STYNAME'].tolist()
x is pandas.Series,  it has just one "column",  "column" with names of states is index.

To extract it and convert to list use x.index.tolist()
Hi wavic and zivoni.
Many thanks for your quick replies.

When I try

df_list = x['CTYNAME'].tolist()

The error reads:
KeyError: 'CTYNAME'
x is pd.Series, it has no key or  column named 'CITYNAME', it has
  • index (with name 'CITYNAME') with values given by group variable (distinct CITYNAMEs)
  • values equal to sums of CENSUS2010POP over groups given by CITYNAMEs
Simple example with fake data and different colnames:
Output:
In [1]: import pandas as pd In [2]:  df = pd.DataFrame({'statename':['California', 'Texas', 'Nevada', 'Hawai', 'California', 'Hawai',    ...:  'Texas'], 'census':[1312, 2343, 3771, 3411, 1211, 943, 2341]}) In [3]: df Out[3]:    census   statename 0    1312  California 1    2343       Texas 2    3771      Nevada 3    3411       Hawai 4    1211  California 5     943       Hawai 6    2341       Texas In [4]: x = df.groupby('statename').census.sum().nlargest(3) In [5]: x Out[5]: statename Texas     4684 Hawai     4354 Nevada    3771 Name: census, dtype: int64 In [6]: largest_states = x.index.tolist() In [7]: largest_states Out[7]: ['Texas', 'Hawai', 'Nevada'] In [8]: x.name Out[8]: 'census' In [9]: x.index.name Out[9]: 'statename'
I see. Awesome! Thank you so much.