Pandas 将一列列表转换为哑元 [英] Pandas convert a column of list to dummies
问题描述
我有一个数据框,其中一列是我的每个用户所属的组列表.类似的东西:
索引组0 ['a','b','c']1 ['c']2 ['b','c','e']3 ['a','c']4 ['b','e']
我想做的是创建一系列虚拟列来标识每个用户属于哪些组,以便进行一些分析
index a b c d e0 1 1 1 0 01 0 0 1 0 02 0 1 1 0 13 1 0 1 0 04 0 1 0 0 0pd.get_dummies(df['groups'])
不起作用,因为这只会为我的列中的每个不同列表返回一列.
解决方案需要高效,因为数据帧将包含 500,000 多行.任何建议将不胜感激!
将 s
用于您的 df['groups']
:
In [21]: s = pd.Series({0: ['a', 'b', 'c'], 1:['c'], 2: ['b', 'c', 'e'], 3: ['a', 'c'], 4: ['b', 'e'] })在 [22]: s出[22]:0 [a, b, c]1 [c]2 [b, c, e]3 [a, c]4 [b, e]数据类型:对象
这是一个可能的解决方案:
在[23]中:pd.get_dummies(s.apply(pd.Series).stack()).sum(level=0)出[23]:a b c e0 1 1 1 01 0 0 1 02 0 1 1 13 1 0 1 04 0 1 0 1
这样做的逻辑是:
.apply(Series)
将一系列列表转换为数据框.stack()
再次将所有内容放在一列中(创建多级索引)pd.get_dummies( )
创建假人.sum(level=0
) 用于重新合并本应为一行的不同行(通过总结第二级,只保留原始级别(level=0
)>))
稍微等效的是 pd.get_dummies(s.apply(pd.Series), prefix='', prefix_sep='').sum(level=0,axis=1)
>
我不知道这是否足够有效,但无论如何,如果性能很重要,将列表存储在数据框中并不是一个好主意.
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.
The solution needs to be efficient as the dataframe will contain 500,000+ rows. Any advice would be appreciated!
Using s
for your df['groups']
:
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
The logic of this is:
.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
))
An slight equivalent is 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屋!