如何使用Pandas Python将字符串拆分为数据帧中的几列? [英] How do I split a string into several columns in a dataframe with pandas Python?

查看:101
本文介绍了如何使用Pandas Python将字符串拆分为数据帧中的几列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以下问题:

1.)如何使用熊猫根据几个字符串索引拆分一列? 2.)如何拆分文本一列分成多行?

我想将它们分成几个新列.假设我有一个看起来像这样的数据框:

I want to split these into several new columns though. Suppose I have a dataframe that looks like this:

id    | string
-----------------------------
1     | astring, isa, string
2     | another, string, la
3     | 123, 232, another

我知道使用:

df['string'].str.split(',')

我可以分割一个字符串.但下一步,我想像这样有效地将拆分后的字符串放入新列中:

I can split a string. But as a next step, I want to efficiently put the split string into new columns like so:

id    | string_1 | string_2 | string_3
-----------------|---------------------
1     | astring  | isa      | string
2     | another  | string   | la
3     | 123      | 232      | another
---------------------------------------

例如,我可以这样做:

for index, row in df.iterrows():
    i = 0
    for item in row['string'].split():
        df.set_values(index, 'string_{0}'.format(i), item)
        i = i + 1

但是,如何才能更优雅地达到相同的结果呢?a

But how could one achieve the same result more elegantly?a

推荐答案

str.split方法具有expand自变量:

>>> df['string'].str.split(',', expand=True)
         0        1         2
0  astring      isa    string
1  another   string        la
2      123      232   another
>>>

带有列名:

>>> df['string'].str.split(',', expand=True).rename(columns = lambda x: "string"+str(x+1))
   string1  string2   string3
0  astring      isa    string
1  another   string        la
2      123      232   another

Python更整洁> = 3.6 f字符串:

Much neater with Python >= 3.6 f-strings:

>>> (df['string'].str.split(',', expand=True)
...              .rename(columns=lambda x: f"string_{x+1}"))
  string_1 string_2  string_3
0  astring      isa    string
1  another   string        la
2      123      232   another

这篇关于如何使用Pandas Python将字符串拆分为数据帧中的几列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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