我有一个Pandas数据框,如下所示:
我希望将
:
id pos value sent
1 a/b/c test/test2/test3 21
2 d/a test/test5 21
我希望将
df ['pos']
和df ['token']
拆分(=explode),使数据框看起来像这样:
id pos value sent
1 a test 21
1 b test2 21
1 c test3 21
2 d test 21
2 a test5 21
如果我将每一列分割并连接起来,它就无法工作,如下所示:
pos = df.token.str.split('/', expand=True).stack().str.strip().reset_index(level=1, drop=True)
df1 = pd.concat([pos,value], axis=1, keys=['pos','value'])
有什么想法吗?非常感谢。
编辑:
我尝试使用这个解决方案:https://dev59.com/cGcs5IYBdhLWcg3wp1pW#40449726
但是我收到以下错误:
TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'
我想这是一个numpy相关的问题,虽然我不确定为什么会这样。我正在使用Python 2.7.14。