【发布时间】:2021-04-23 06:53:06
【问题描述】:
我有一个如下所示的数据框
id value index
5eb3cbcc434474213e58b49a [1,2,3,4,6] [0,1,2,3,4]
5eb3f335434474213e58b49d [1,2,3,4] [0,2,3,4]
5eb3f853434474213e58b49f [1,2,3,4] [0,2,3,4]
5eb40395434474213e58b4a2 [1,2,3,4] [0,1,2,3]
5eb40425434474213e58b4a5 [1,2] [0,2]
我尝试在以下内容中转换此数据框,因为索引旨在作为每个单独值的标题,看起来像这样:
id 0 1 2 3 4
5eb3cbcc434474213e58b49a 1 2 3 4 6
5eb3f335434474213e58b49d 1 Nan 2 3 4
5eb3f853434474213e58b49f 1 Nan 2 3 4
5eb40395434474213e58b4a2 1 2 3 4 Nan
5eb40425434474213e58b4a5 1 Nan 2 Nan Nan
我尝试首先拆分列表列表:
new_df = pd.DataFrame(df.Value.str.split(',').tolist(), index=df.Index).stack()
new_df = new_df.reset_index([0, 'Index'])
new_df.columns = ['Value', 'Index']
但是我收到了错误
TypeError: unhashable type: 'list'
是什么导致了这个错误?
【问题讨论】:
-
以下答案是否符合您的要求?如果是,请查看答案并选择一个给accept the answer。这是 StackOverflow 帮助其他有类似问题的用户获得解决方案的方式。谢谢! :-)