Convert One Column in Python Dataframe to List

18,940

Just do:

>>> list(df_gearME.ColA)
[341321432, 132184900, 173840143, 1432473928]

Or print it for horizontal output:

>>> print(list(f_gearME.ColA))
[341321432, 132184900, 173840143, 1432473928]
Share:
18,940

Related videos on Youtube

PineNuts0
Author by

PineNuts0

Updated on June 04, 2022

Comments

  • PineNuts0
    PineNuts0 almost 2 years

    I have a pandas dataframe consisting of only one column of data. I want to convert the column of data into a list. The column has float datatype.

    For example:

    ColA
    341321432
    132184900
    173840143
    1432473928
    

    Desired: 341321432, 132184900, 173840143, 1432473928

    Below is my Python code:

    df_gearME = pd.read_excel('Gear M&Es.xlsx')
    df_gearME['ColA'].to_list()
    

    But the error I get is as follows:

    AttributeError: 'Series' object has no attribute 'to_list'
    
    • Jon Clements
      Jon Clements about 6 years
      The method is tolist() - no _
    • PineNuts0
      PineNuts0 about 6 years
      ah, I see ... that gives me the values listed out vertically ... how do I get the output horizontally?
    • Jon Clements
      Jon Clements about 6 years
      It's just a list - how the environment you're in chooses to display it is something different - ipython/jupyter will try to pretty print it for instance... if you want to display it in a certain way - you have to format it in a certain way...
    • Nicholas Nelson
      Nicholas Nelson about 5 years
      @JonClements you're right that the method is tolist() but the pandas 0.24.0 documentation says it's to_list(). Do you know why the documentation lists it with an _?
    • Jon Clements
      Jon Clements about 5 years
      @NicholasNelson nope... sorry... not a developer for either libraries.
    • MrRaghav
      MrRaghav over 3 years
      @JonClements thank you so very much!
  • PineNuts0
    PineNuts0 about 6 years
    when I use the above code, it provides the output vertically .... anyway I can get it horizontally?