2

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
PyBoss
  • 599
  • 1
  • 7
  • 20

3 Answers3

2

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']
Marcelo Paco
  • 2,732
  • 4
  • 9
  • 26
1
import pandas as pd
d = { "letter": ["a", "b", "c"]}
df = pd.DataFrame(d)

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

Output:

['a', 'b', 'c']
TimbowSix
  • 342
  • 1
  • 7
0
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)
mks2192
  • 306
  • 2
  • 11