小编Aki*_*aka的帖子

如何将列中的列表转换为垂直形状?

我的pandas数据框中的一列包含一个列表.我想扩展它并转换如下的垂直形状.怎么做?

前(代码):

import pandas as pd
pd.DataFrame({
    'col1':['fruit', 'veicle', 'animal'],
    'col2':['apple', 'bycicle', 'cat'],
    'col3':[1,4,2],
    'list':[
        [10, 20],
        [1.2, 3.0, 2.75],
        ['tommy', 'tom']
    ]
})
Run Code Online (Sandbox Code Playgroud)

前(表):

    |col1  |col2   |col3|list            |
    |------|-------|----|----------------|
    |fruit |apple  |   1|[10, 20]        |
    |veicle|bicycle|   4|[1.2, 3.0, 2.75]|
    |animal|cat    |   2|['tommy', 'tom']|
Run Code Online (Sandbox Code Playgroud)

    |col1  |col2   |col3|list   |
    |------|-------|----|-------|
    |fruit |apple  |   1|10     |
    |fruit |apple  |   1|20     |
    |viecle|bycicle|   4|1.2    |
    |viecle|bycicle|   4|3.0    |
    |viecle|bycicle|   4|2.75   |
    |animal|cat    |   2|'tommy'|
    |animal|cat    |   2|'tom   |
Run Code Online (Sandbox Code Playgroud)

注1:列表的长度和类型不同.

注2:我可以修改代码生成datafarme. …

python reshape dataframe pandas

2
推荐指数
1
解决办法
516
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1

reshape ×1