Convert dataframe to list of record WITHOUT brackets

Question:

I have a dataframe

import pandas as pd
d = { "letter": ["a", "b", "c"]}
df = pd.DataFrame(d)

enter image description here

if I use df.values.tolist() , I will get

enter image description here

BUT my expected result is below

letter =['a','b','c']

which without the brackets

Asked By: PyBoss

||

Answers:

import pandas as pd
d = { "letter": ["a", "b", "c"]}
df = pd.DataFrame(d)

df.values.ravel().tolist()

['a', 'b', 'c']

if you print below then it’ll be clear

print(df.values)

array([['a'],
   ['b'],
   ['c']], dtype=object)
Answered By: manoj
import pandas as pd
d = { "letter": ["a", "b", "c"]}
df = pd.DataFrame(d)

df["letter"].values.tolist()

Output:

['a', 'b', 'c']
Answered By: Timbow Six

You want to use the to_list() function after indexing the column of interest:

import pandas as pd
d = { "letter": ["a", "b", "c"]}
df = pd.DataFrame(d)

# Call to_list() in column of interest
letter = df.letter.to_list()

letter variable now holds:

['a', 'b', 'c']
Answered By: Marcelo Paco