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

标签 python pandas dataframe

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

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'

是什么导致了这个错误?

最佳答案

您可以使用 .apply()连同 pd.Series() ,如下:

df = df.set_index('id').apply(lambda x: pd.Series(x['value'], index=x['index']), axis=1).reset_index()


print(df)

                         id    0    1    2    3    4
0  5eb3cbcc434474213e58b49a  1.0  2.0  3.0  4.0  6.0
1  5eb3f335434474213e58b49d  1.0  NaN  2.0  3.0  4.0
2  5eb3f853434474213e58b49f  1.0  NaN  2.0  3.0  4.0
3  5eb40395434474213e58b4a2  1.0  2.0  3.0  4.0  NaN
4  5eb40425434474213e58b4a5  1.0  NaN  2.0  NaN  NaN

这利用了.apply()功能特点:

The default behaviour (None) depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these are expanded to columns.

此功能非常方便,可帮助我们为需要将数据扩展至列的问题提供简单的解决方案,同时通过保留现有行索引并将其代代到这些新列,将新列合并到现有数据中。我用它来提供 simple answer回答一个经典问题:How to merge a Series and DataFrame .

关于python - 在一个单元格中转换具有多个值的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67225274/

相关文章:

Pandas:按四舍五入的 float 分组

python - 根据 Pandas 数据框中2列的数据计算值

python - Bottle 路由处理 POST 和 GET

python - 将数据框列表转换(转置)为列

python - 读取Python中一行中的第二个 float

python - 如何在 python pandas 中标记循环数的值

python - 将 pandas DataFrame 制作成 dict 和 dropna

python - 将数据帧转换为字典,忽略某些值

r - 在整个数据框中查找多个字符串

R:如何使用应用于 data.frame 列的 st_point() 创建点?