小编EQT*_*999的帖子

在一个单元格中转换具有多个值的数据帧

我有一个如下所示的数据框

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]
Run Code Online (Sandbox Code Playgroud)

我尝试在以下内容中转换此数据框,因为索引旨在作为每个单独值的标题,看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)

我尝试首先拆分列表列表:

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']
Run Code Online (Sandbox Code Playgroud)

但是我收到错误

类型错误:不可散列的类型:“列表”

是什么导致了这个错误?

python dataframe pandas

3
推荐指数
1
解决办法
61
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1