python用逗号将文本分成不同的列 [英] python separate text into different column with comma

查看:117
本文介绍了python用逗号将文本分成不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中提取数据,并将其写入报告的新Excel文件中.我的问题是,数据的最后一列包含用逗号分隔的数据,需要将其分隔成单独的列.

I'm pulling data from a database and writing to a new Excel file for a report. My issue is that the last column of data has data that is separated by commas and needs to be separated into separate columns.

作为一个例子,我有如下数据:

As an example I have data like the following:

Name  Info
Mike  "a, b, c, d"
Joe  "a, f, z"

我需要将这些字母分成不同的列. a,b等不必排队,因此每个字母都在正确"列中.他们只需要分解成单独的列即可.

I need to break these letters out into separate columns. The a's, b's, etc. don't have to line up so that each letter is in the "correct" column. They just need to be broken out into separate columns.

我正在Python中执行此操作.我愿意使用其他库,例如Pandas.还将包括其他列,而不仅仅是两列.我举了一个简单的例子.

I'm doing this in Python. I'm open to using other libraries like Pandas. There will be other columns included, not just two. I made a simple example.

感谢您的帮助.

推荐答案

从熊猫str.split

df=pd.concat([df,df.Info.str.split(',',expand=True)],1)
df
Out[611]: 
   Name        Info  0   1   2     3
0  Mike  a, b, c, d  a   b   c     d
1   Joe     a, f, z  a   f   z  None

这篇关于python用逗号将文本分成不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆