pandas 将列表的一列转换为假人 [英] Pandas convert a column of list to dummies
问题描述
我有一个数据框,其中一列是我的每个用户所属的组的列表.像这样:
I have a dataframe where one column is a list of groups each of my users belongs to. Something like:
index groups
0 ['a','b','c']
1 ['c']
2 ['b','c','e']
3 ['a','c']
4 ['b','e']
我想做的是创建一系列虚拟列,以识别每个用户所属的组,以便进行一些分析
And what I would like to do is create a series of dummy columns to identify which groups each user belongs to in order to run some analyses
index a b c d e
0 1 1 1 0 0
1 0 0 1 0 0
2 0 1 1 0 1
3 1 0 1 0 0
4 0 1 0 0 0
pd.get_dummies(df['groups'])
无效,因为这只会为我的列中的每个不同列表返回一列.
won't work because that just returns a column for each different list in my column.
该解决方案需要高效,因为数据框将包含500,000+行.任何建议,将不胜感激!
The solution needs to be efficient as the dataframe will contain 500,000+ rows. Any advice would be appreciated!
推荐答案
为您的df['groups']
使用s
:
In [21]: s = pd.Series({0: ['a', 'b', 'c'], 1:['c'], 2: ['b', 'c', 'e'], 3: ['a', 'c'], 4: ['b', 'e'] })
In [22]: s
Out[22]:
0 [a, b, c]
1 [c]
2 [b, c, e]
3 [a, c]
4 [b, e]
dtype: object
这是一个可能的解决方案:
This is a possible solution:
In [23]: pd.get_dummies(s.apply(pd.Series).stack()).sum(level=0)
Out[23]:
a b c e
0 1 1 1 0
1 0 0 1 0
2 0 1 1 1
3 1 0 1 0
4 0 1 0 1
其逻辑是:
-
.apply(Series)
将一系列列表转换为数据框 -
.stack()
将所有内容再次放入一列(创建多级索引) -
pd.get_dummies( )
创建假人 -
.sum(level=0
)用于合并应为一行的不同行(通过汇总第二级,仅保留原始级(level=0
))
.apply(Series)
converts the series of lists to a dataframe.stack()
puts everything in one column again (creating a multi-level index)pd.get_dummies( )
creating the dummies.sum(level=0
) for remerging the different rows that should be one row (by summing up the second level, only keeping the original level (level=0
))
略微等于pd.get_dummies(s.apply(pd.Series), prefix='', prefix_sep='').sum(level=0, axis=1)
我不知道这是否足够有效,但是无论如何,如果性能很重要,那么将列表存储在数据框中并不是一个好主意.
If this will be efficient enough, I don't know, but in any case, if performance is important, storing lists in a dataframe is not a very good idea.
这篇关于 pandas 将列表的一列转换为假人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!