如何在python中拆分excel表中写的句子?

时间:2018-05-02 16:46:28

标签: python excel split

我有这张excel表:

enter image description here

从第一张excel表中我想制作另一张excel表:

enter image description here

这里是用于拆分单个句子的python代码,但是我无法使用excel表进行拆分。

    import xlrd
    import pandas as pd
    b=xlrd.open_workbook("sample_docu5.xlsx")
    p=b.sheet_by_index(0)
    #open("sample_docu5.xlsx") as f:
    s=  "Dead poet society, Julia Roberts, London"
    line=s.split(',')
    print (line)

输出:

['Dead poet society', ' Julia Roberts', ' London']

1 个答案:

答案 0 :(得分:0)

使用Pandas,您可以读取excel文件,拆分列,将它们存储在pandas数据帧中,将该数据帧写入新的Excel文件。

import pandas as pd
#Read in your dataset with the 2 headers
df = pd.read_excel(r'sample_docu5.xlsx')

#Split out the first column into 3 different columns
df['Title'], df['Actor'],df['Place'] = df['All'].str.split(',', 2).str

#Delete the 'All' column as we have created 3 new columns
del df['All']

#Reorder the columns
df = df[['Title','Actor','Place','Document_Source']]
df.to_excel('output.xlsx')
df.head()