pandas series to list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: pandas series to list (/thread-2133.html) |
pandas series to list - metalray - Feb-21-2017 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: int64I 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. RE: pandas series to list - wavic - Feb-21-2017 df_list = df['STYNAME'].tolist() RE: pandas series to list - zivoni - Feb-21-2017 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() RE: pandas series to list - metalray - Feb-21-2017 Hi wavic and zivoni. Many thanks for your quick replies. When I try df_list = x['CTYNAME'].tolist() The error reads: KeyError: 'CTYNAME' RE: pandas series to list - zivoni - Feb-21-2017 x is pd.Series, it has no key or column named 'CITYNAME', it has
RE: pandas series to list - metalray - Feb-21-2017 I see. Awesome! Thank you so much. |