我有多个数据帧列,如下所示:
Day1
0 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
1 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
2 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
3 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
4 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
我想要的是每个角色都在一个单独的列中分开:
012345678910111213....
0 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
1 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
2 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
3 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
4 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
这样,“第1天的列”被分成48列,每列都有一个值a/b/c/d
我试过分开,但没用。
最佳答案
您可以调用apply
并在值的pd.Series
上为每一行调用list
In [16]:
df['Day1'].apply(lambda x: pd.Series(list(x)))
Out[16]:
0 1 2 3 4 5 6 7 8 9 ... 38 39 40 41 42 43 44 45 46 47
0 D D D D D D D D D D ... D D D D D D D D D D
1 D D D D D D D D D D ... D D D D D D D D D D
2 D D D D D D D D D D ... D D D D D D D D D D
3 D D D D D D D D D D ... D D D D D D D D D D
4 D D D D D D D D D D ... D D D D D D D D D D
[5 rows x 48 columns]
似乎有尾随空格,请使用
str.rstrip
删除这些空格:df['Day1'] = df['Day1'].str.rstip()
那么做上面的