pandas 使用 python 将文本到带有逗号分隔符的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39553392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:01:39  来源:igfitidea点击:

text to columns with comma delimiter using python

pythonpython-3.xpandasopenpyxl

提问by ak_1

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

如何在excel中使用分隔符','将一列分成两列,并使用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][1]

[输入文件链接][1]

[outputfile][3]

[输出文件][3]

采纳答案by shawnheide

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):

输出(在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