Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a dataframe
import pandas as pd d = { "letter": ["a", "b", "c"]} df = pd.DataFrame(d)
if I use df.values.tolist() , I will get
df.values.tolist()
BUT my expected result is below
letter =['a','b','c']
which without the brackets
You want to use the to_list() function after indexing the column of interest:
to_list()
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:
letter
['a', 'b', 'c']
Add a comment
import pandas as pd d = { "letter": ["a", "b", "c"]} df = pd.DataFrame(d) df["letter"].values.tolist()
Output:
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)
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.