【发布时间】:2020-11-11 05:24:53
【问题描述】:
在一个大熊猫数据框中,我有三列(fruit、vegetable 和 first_name)。这些列的值是列表。
从列表中,我想为 DataFrame 的每一行创建一个包含字典列表的新列。
我有三列(fruit、vegetable 和 first_name),每一行都有列表作为它们的值。
我的数据框的第一行:
df = pd.DataFrame({
"fruit": [["Apple", "Banana","Pear","Grape","Pineapple"]],
"vegetable": [["Celery","Onion","Potato","Broccoli","Sprouts"]],
"first_name": [["Sam", "Beth", "John", "Daisy", "Jane"]]
})
如何将三列转换为一列并使值看起来像这样?
[
{"fruit": "Apple", "vegetable":"Celery", "first_name":"Sam"},
{"fruit": "Banana", "vegetable":"Onion", "first_name":"Beth"},
{"fruit": "Pear", "vegetable":"Potato", "first_name":"John"},
{"fruit": "Grape", "vegetable":"Broccoli", "first_name":"Daisy"},
{"fruit": "Pineapple", "vegetable":"Sprouts", "first_name":"Jane"}
]
【问题讨论】: