Python Forum
pandas series to list
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas series to list
#1
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.
Reply
#2
df_list = df['STYNAME'].tolist()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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()
Reply
#4
Hi wavic and zivoni.
Many thanks for your quick replies.

When I try

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

The error reads:
KeyError: 'CTYNAME'
Reply
#5
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'
Reply
#6
I see. Awesome! Thank you so much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fit straight line to pandas time series data with semilog plot schniefen 2 1,531 Mar-10-2023, 01:08 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020