4

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

This
  • 143
  • 1
  • 2
  • 8

2 Answers2

10

Use Series.tolist:

list_text = df.text.tolist()

print(list_text)

['Hello', 'How are you', 'From', 'Liban']
Erfan
  • 40,971
  • 8
  • 66
  • 78
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)
Rebin
  • 516
  • 1
  • 6
  • 16
  • 1
    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](https://stackoverflow.com/help/how-to-answer) – Anthony Apr 25 '19 at 00:12
  • I will add description – Rebin Apr 25 '19 at 15:07