我有一个下面的嵌套列表:
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']
如何将每个值列表转置到该位置的新行中?
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]}')
假设此输入并且您想要转换为 pandas 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
第一:
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