如何在python中拆分excel工作表中编写的句子?
我有这张excel表格: 从第一张excel表格开始,我想制作另一张类似这样的excel表格: 下面是分割一个句子的python代码,但我无法使用excel工作表进行分割如何在python中拆分excel工作表中编写的句子?,python,excel,split,Python,Excel,Split,我有这张excel表格: 从第一张excel表格开始,我想制作另一张类似这样的excel表格: 下面是分割一个句子的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
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']
使用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()
del df['All']的作用是什么它删除了1列中存储有['Dead poeter society'、'Julia Roberts'、'London']的1列。但是上面的代码将该列拆分为3列,您能告诉我如何更改您提供的代码以获得所需的输出吗?请参阅我提供的两个截图。使用我提供的代码,在pandas中创建数据帧可以轻松操作