将dataframe列转换为字符串列表

时间:2019-04-24 21:54:17

标签: python pandas dataframe

从数据框列中删除字符串列表,如我的代码所示:

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

我的列表将有

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

谢谢

2 个答案:

答案 0 :(得分:1)

使用Series.tolist

list_text = df.text.tolist()

print(list_text)

['Hello', 'How are you', 'From', 'Liban']

答案 1 :(得分:0)

您可以遍历目标列中的元素,并将所需的元素添加到空白列表中。

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)