text to columns with comma delimiter using python

ⅰ亾dé卋堺 提交于 2019-12-12 03:45:15

问题


how to divide a column in to two columns in an excel using a delimiter ', 'and name the header's using python

here is my code

import openpyxl


w=openpyxl.load_workbook('DDdata.xlsx')
active=w.active


a=active.columns[1]
for cellobj in a:
    s=cellobj.value
    fila=s.split(',')

    print(fila)

Input file link

outputfile


回答1:


This looks to be a duplicate of this post, but here's a solution for you to consider:

import pandas as pd

df = pd.read_excel('input.xlsx')

df['First Name'], df['Last Name'] = df['Applicant Name'].str.split(',', 1).str
del df['Applicant Name']

df.to_excel('output.xlsx')

Output (in python):

First Name  Last Name
0   -   Mohammed Abdul Naser Aziz
1   -   Gottipati Harshavardhan
2   -   Sandeep Kaur
3   .   Rounak
4   abbas   Syed
5   Abbas   Mohd Manzar
6   Abbasi  Fatema
7   Abdelkader  BEKHTIE
8   abdollahzadehkan    mina


来源:https://stackoverflow.com/questions/39553392/text-to-columns-with-comma-delimiter-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!