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]
Related videos on Youtube
Author by
PineNuts0
Updated on June 04, 2022Comments
-
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 about 6 yearsThe method is
tolist()
- no_
-
PineNuts0 about 6 yearsah, I see ... that gives me the values listed out vertically ... how do I get the output horizontally?
-
Jon Clements about 6 yearsIt'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 about 5 years@JonClements you're right that the method is
tolist()
but the pandas 0.24.0 documentation says it'sto_list()
. Do you know why the documentation lists it with an_
? -
Jon Clements about 5 years@NicholasNelson nope... sorry... not a developer for either libraries.
-
MrRaghav over 3 years@JonClements thank you so very much!
-
-
PineNuts0 about 6 yearswhen I use the above code, it provides the output vertically .... anyway I can get it horizontally?