将项目列表转换为 pandas 中的虚拟对象
Convert a list of lists of items to dummies in pandas
我有一个这样的项目列表:
lgenre[8:15]
[['Action'],
['Action', 'Adventure', 'Thriller'],
['Comedy', 'Drama', 'Romance'],
['Comedy', 'Horror'],
['Animation', "Children's"],
['Drama'],
['Action', 'Adventure', 'Romance']]
我想要的是:
id Action Adventure Thriller Comedy Drama Romance Horror Animation Children's
0 0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
2 2 0 0 0 1 1 1 0 0 0
3 3 0 0 0 1 0 0 1 0 0
4 4 0 0 0 0 0 0 0 1 1
5 5 0 0 0 0 1 0 0 0 0
6 6 1 1 0 0 0 1 0 0 0
我尝试编写一个双循环,如下所示:
stor=pd.DataFrame({'id':list(range(len(lgenre[8:15])))})
for num,list in enumerate(lgenre[8:15]):
for item in list:
try:
stor[item][num]=1
except:
stor[item]=0
stor[item][num]=1
虽然可以编译,但是实现起来太慢了。
有没有有效的方法来做这种事情?
有更好的算法或内置方法吗?
从嵌套列表构建数据框,并使用 pd.get_dummies
:
df = pd.get_dummies(pd.DataFrame(l))
df.columns = df.columns.str.split("_").str[-1]
Action Animation Comedy Drama Adventure Children's Drama Horror \
0 1 0 0 0 0 0 0 0
1 1 0 0 0 1 0 0 0
2 0 0 1 0 0 0 1 0
3 0 0 1 0 0 0 0 1
4 0 1 0 0 0 1 0 0
5 0 0 0 1 0 0 0 0
6 1 0 0 0 1 0 0 0
Romance Thriller
0 0 0
1 0 1
2 1 0
3 0 0
4 0 0
5 0 0
6 1 0
我有一个这样的项目列表:
lgenre[8:15]
[['Action'],
['Action', 'Adventure', 'Thriller'],
['Comedy', 'Drama', 'Romance'],
['Comedy', 'Horror'],
['Animation', "Children's"],
['Drama'],
['Action', 'Adventure', 'Romance']]
我想要的是:
id Action Adventure Thriller Comedy Drama Romance Horror Animation Children's
0 0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
2 2 0 0 0 1 1 1 0 0 0
3 3 0 0 0 1 0 0 1 0 0
4 4 0 0 0 0 0 0 0 1 1
5 5 0 0 0 0 1 0 0 0 0
6 6 1 1 0 0 0 1 0 0 0
我尝试编写一个双循环,如下所示:
stor=pd.DataFrame({'id':list(range(len(lgenre[8:15])))})
for num,list in enumerate(lgenre[8:15]):
for item in list:
try:
stor[item][num]=1
except:
stor[item]=0
stor[item][num]=1
虽然可以编译,但是实现起来太慢了。 有没有有效的方法来做这种事情? 有更好的算法或内置方法吗?
从嵌套列表构建数据框,并使用 pd.get_dummies
:
df = pd.get_dummies(pd.DataFrame(l))
df.columns = df.columns.str.split("_").str[-1]
Action Animation Comedy Drama Adventure Children's Drama Horror \
0 1 0 0 0 0 0 0 0
1 1 0 0 0 1 0 0 0
2 0 0 1 0 0 0 1 0
3 0 0 1 0 0 0 0 1
4 0 1 0 0 0 1 0 0
5 0 0 0 1 0 0 0 0
6 1 0 0 0 1 0 0 0
Romance Thriller
0 0 0
1 0 1
2 1 0
3 0 0
4 0 0
5 0 0
6 1 0