Python 中的嵌套列表 - 转置嵌套列表

问题描述 投票:0回答:3

我有一个下面的嵌套列表:

list = [Language:'Tamil'
        Capital: 'Chennai' 
        Place: 'Chennai', 'Vellore', 'Trichy', 'Madurai' 
]

我希望将其转换为:

Language     Capital     Place
Tamil        Chennai     Chennai
Tamil        Chennai     Vellore
Tamil        Chennai     Trichy
Tamil        Chennai     Madurai 

尝试转换为 pandas 数据框:

df = pd.DataFrame(list)

结果是

Language    Capital       Place  
Tamil       Chennai       ['Chennai', 'Vellore', 'Trichy', 'Madurai']  

如何将每个值列表转置到该位置的新行中?

python list
3个回答
2
投票
from itertools import product

data = {
    "Language": "Tamil",
    "Capital": "Chennai",
    "Place": ["Chennai", "Vellore", "Trichy", "Madurai"]
}

results = list(product([data["Language"]], [data["Capital"]], data["Place"]))

for result in results:
    print(f'{result[0]: <10} {result[1]: <10} {result[2]}')

0
投票

假设此输入并且您想要转换为 DataFrame:

data = {
    'Language': 'Tamil',
    'Capital': 'Chennai',
    'Place': ['Chennai', 'Vellore', 'Trichy', 'Madurai']
}

如果您知道包含列表的键,只需

explode
它:

import pandas as pd

out = pd.DataFrame(data).explode('Place')

如果您想以编程方式识别包含列表的键:

from itertools import product

out = pd.DataFrame(product(*(v if isinstance(v, list) else [v]
                             for v in data.values())),
                   columns=data
                  )

输出:

  Language  Capital    Place
0    Tamil  Chennai  Chennai
1    Tamil  Chennai  Vellore
2    Tamil  Chennai   Trichy
3    Tamil  Chennai  Madurai

-1
投票

第一:

data = {
    'Language': 'Tamil',
    'Capital': 'Chennai',
    'Place': ['Chennai', 'Vellore', 'Trichy', 'Madurai']
}

接下来,您可以通过重复 Language 和 Capital 的值并将 Place 列表扩展为多行来创建 DataFrame:

df = pd.DataFrame({
    'Language': [data['Language']] * len(data['Place']),
    'Capital': [data['Capital']] * len(data['Place']),
    'Place': data['Place']
})

输出:

     Language  Capital    Place
0    Tamil     Chennai    Chennai
1    Tamil     Chennai    Vellore
2    Tamil     Chennai    Trichy
3    Tamil     Chennai    Madurai
© www.soinside.com 2019 - 2024. All rights reserved.