我有一个数据帧拆分设置为多列大熊猫的Python
IDs Types
0 1001 {251}
1 1013 {251, 101}
2 1004 {251, 701}
3 3011 {251}
4 1014 {701}
5 1114 {251}
6 1015 {251}
其中df['Types']
具有组,每个组一行。我想这列转换为多列,这样我可以得到下面的输出
IDs Type1 Type2
0 1001 251 -
1 1013 251 101
2 1004 251 701
3 3011 251 -
4 1014 701 -
5 1114 251 -
6 1015 251 -
目前,我使用下面的代码来实现这一
pd.concat([df['Types'].apply(pd.Series), df['IDs']], axis = 1)
但它返回下面的错误
Traceback (most recent call last):
File "C:/Users/PycharmProjects/test/test.py", line 48, in <module>
df = pd.concat([df['Types'].apply(pd.Series), df['IDs']], axis = 1)
File "C:\Python\Python35\lib\site-packages\pandas\core\series.py", line 2294, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas\src\inference.pyx", line 1207, in pandas.lib.map_infer (pandas\lib.c:66124)
File "C:\Python\Python35\lib\site-packages\pandas\core\series.py", line 223, in __init__
"".format(data.__class__.__name__))
TypeError: 'set' type is unordered
请指导我如何获得所需的输出。谢谢
感谢。我在想为什么我需要把这个集合转换成列表? – muazfaiz
我不确定,但这个解决方案更快,因为'.apply(Series)',但'.apply(lambda x:pd.Series(list(x)))'可以工作。 – jezrael