我在 stackoverflow 中阅读了该问题的解决方案,但没有人具体说明何时需要分隔多个列。例如:
输出
movieId genres
1 Adventure|Animation|Children|Comedy|Fantasy
2 Adventure|Children|Fantasy
3 Comedy|Romance
4 Comedy|Drama|Romance
5 Comedy
6 Action|Crime|Thriller
7 Comedy|Romance
我怎样才能用 Pandas 做到这一点?
最佳答案
使用dot
列名带有 |
并通过 rstrip
删除最后一个 |
:
print (df1)
movieId Action Adventure Animation Children Comedy Crime Drama \
0 1 0 1 1 1 1 0 0
1 2 0 1 0 1 0 0 0
2 3 0 0 0 0 1 0 0
3 4 0 0 0 0 1 0 1
4 5 0 0 0 0 1 0 0
5 6 1 0 0 0 0 1 0
6 7 0 0 0 0 1 0 0
Fantasy Romance Thriller
0 1 0 0
1 1 0 0
2 0 1 0
3 0 1 0
4 0 0 0
5 0 0 1
6 0 1 0
df = df1.set_index('movieId')
df2 = df.dot(df.columns + '|').str.rstrip('|').reset_index(name='genres')
print (df2)
movieId genres
0 1 Adventure|Animation|Children|Comedy|Fantasy
1 2 Adventure|Children|Fantasy
2 3 Comedy|Romance
3 4 Comedy|Drama|Romance
4 5 Comedy
5 6 Action|Crime|Thriller
6 7 Comedy|Romance
关于python - 如何按行重建虚拟数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239832/