在pandas数据框中获取一列字符串数据,并将其拆分为单独的列 [英] Take column of string data in pandas dataframe and split into separate columns

查看:526
本文介绍了在pandas数据框中获取一列字符串数据,并将其拆分为单独的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从CSV读取的数据中有一个pandas数据框.一列是组的名称,而另一列包含一个字符串(看起来像一个列表),如下所示:

I have a pandas dataframe from data I read from a CSV. One column is for the name of a group, while the other column contains a string (that looks like a list), like the following:

Group      |  Followers
------------------------------------------
biebers    |  u'user1', u'user2', u'user3'
catladies  |  u'user4', u'user5'
bkworms    |  u'user6', u'user7'

我想尝试在跟随者"列中拆分字符串,并创建一个单独的数据框,其中每一行用于用户,以及一列显示其所在的组.因此,在此示例中我想得到以下内容:

I'd like to try to split up the strings in the "Followers" column and make a separate dataframe where each row is for a user, as well as a column showing which group they're in. So for this example I'd like to get the following:

User       |     Group
--------------------------------
user1      |     biebers
user2      |     biebers
user3      |     biebers
user4      |     catladies
user5      |     catladies
user6      |     bkworms
user7      |     bkworms

有人对解决此问题的最佳方法提出建议吗?这是它的屏幕截图:

Anyone have suggestions for the best way to approach this? Here's a screenshot of what it looks like:

推荐答案

df.Followers = df.Followers.str.replace(r"u'([^']*)'", r'\1')

df.set_index('Group').Followers.str.split(r',\s*', expand=True) \
  .stack().rename('User').reset_index('Group').set_index('User')

User保留为列.

df.Followers = df.Followers.str.replace(r"u'([^']*)'", r'\1')

df.set_index('Group').Followers.str.split(r',\s*', expand=True) \
  .stack().rename('User').reset_index('Group') \
  .reset_index(drop=True)[['User', 'Group']]

这篇关于在pandas数据框中获取一列字符串数据,并将其拆分为单独的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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