5

bust a list of string from a dataframe column like shown with my code:

d = {'text': ["Hello", "How are you","From","Liban"]}
df = pd.DataFrame(data=d)
df

My list will have

    List_text = ["Hello","How are you","From","Liban"].

Thank you

0

2 Answers 2

14

Use Series.tolist:

list_text = df.text.tolist()

print(list_text)

['Hello', 'How are you', 'From', 'Liban']
Sign up to request clarification or add additional context in comments.

3 Comments

Hey @Erfan I have to make a test speed on these two ^^
Sure, go ahead. Quite curious as well :)
Same stuff 831ms against 823ms with +/- 30ms so it can be a CPU performance, can't tell, but this is pretty much the same speed, I will upvote yours ;)
0

you can iterate over elements in the target column and add the elements you want to an empty list.

d = {'text': ["Hello", "How are you","From","Liban"]}
df = pd.DataFrame(d)
rows,columns=df.shape

list_text=[] #your empty list 
for index in range(rows): #iteration over the dataframe
    list_text.append(df.iat[index,0])

print(list_text)

2 Comments

While this command may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. How to Answer
I will add description

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.