我有如下字典:
d = {("Sam","Scotland","23") : 25,
("Oli","England","23") : 28,
("Ethan","Wales","18") : 19}
我想将其转换为如下所示的熊猫数据框:
Name Country Age Count
Sam Scotland 23 25
Oli England 23 28
Ethan Wales 18 19
我试图这样做:
df = pd.DataFrame.from_items(d.items(),orient="index",
columns=["Name","Country","Age","Count"])
但是我得到这个错误:
ValueError: The value in each (key, value) pair must be an array, Series, or dict
我理解通过遍历元组的每个项目和每个元素都是可能的,但是有没有更清洁的方法呢?
答案 0 :(得分:4)
您可以通过键和值的连接来手动构建DataFrame的行,例如:
import pandas as pd
d = {("Sam", "Scotland", "23"): 25,
("Oli", "England", "23"): 28,
("Ethan", "Wales", "18"): 19}
df = pd.DataFrame([k + (v,) for k, v in d.items()], columns=['name', 'country', 'age', 'count'])
print(df)
输出
name country age count
0 Ethan Wales 18 19
1 Sam Scotland 23 25
2 Oli England 23 28
或者作为替代:
import pandas as pd
d = {("Sam", "Scotland", "23"): 25,
("Oli", "England", "23"): 28,
("Ethan", "Wales", "18"): 19}
df = pd.DataFrame(
[{"name": name, "country": country, "age": age, "count": value} for (name, country, age), value in d.items()])
print(df)
输出
age count country name
0 23 28 England Oli
1 23 25 Scotland Sam
2 18 19 Wales Ethan
这个想法是将键值对转换成字典列表,然后将其传递给DataFrame
。
答案 1 :(得分:0)
您可以直接将pd.DataFrame.from_dict
与字典 一起使用。这将从您的输入字典键中创建一个tuple
键。您可以将其转换为MultiIndex
,然后使用reset_index
:
cols = ['Name', 'Country', 'Age', 'Count']
df = pd.DataFrame.from_dict(d, orient='index', columns=cols[-1])
df.index = pd.MultiIndex.from_tuples(df.index, names=cols[:-1])
df = df.reset_index()