如何将excel列转换为python中逗号分隔的一个列表

时间:2019-11-13 08:37:12

标签: python excel pandas dataframe

我正在尝试将excel列转换为以逗号分隔的数据框列表, Here is the excel sheet

那是我正在尝试的代码:

df=pd.read_excel('eng_test.xlsx',"Sheet1")
new_df = pd.DataFrame(df) 
for column in new_df:
    new_df2=new_df.apply(pd.Series)
    new_df3=(new_df2.to_string())
    new_df4=new_df3.split()
#print(new_df4)

我希望df像这样 df = pd.DataFrame(['This is my first sentence', 'This is a second sentence with more words'])

1 个答案:

答案 0 :(得分:1)

尽管不清楚用Excel列或列表中的行来实现数据框的真正目的,您可以使用header=None做类似的事情:

df=pd.read_excel('text_excel.xlsx', header=None)
print("\nDataframe: ")
print(df)
df_list = df.values.tolist()
print("\nList: ")
print(df_list)

通过此操作,您将获得以下输出:

Dataframe: 
                                           0
0                'This is my first sentence'
1  This is a second sentence with more words

List: 
[["'This is my first sentence'"], ['This is a second sentence with more words']]