[英]Creating a Pandas DataFrame from a Dictionary of different-length list of Tuples
I have a dictionary of format: Keys = Person's name Items = list(Tuple(GUID, INT(value)), ...)我有一个格式字典: Keys = Person's name Items = list(Tuple(GUID, INT(value)), ...)
{'Person A':[('abc123',1),('bcc222',2),('igh643',1)],'Person B': [('abc123',4)],'Person C': [('abc123',2),('icy558',7)]}
and I would like to turn this into a pandas dataframe of the format Index: GUID, Columns: Person's Name, values: INT(value).我想把它变成一个格式为 Index: GUID, Columns: Person's Name, values: INT(value) 的 Pandas 数据框。 This should look something like:这应该类似于:
I have tried:我试过了:
pd.DataFrame(dict([(k,pd.Series(v)) for k,v in d.items()]))
but it results in:但结果是:
You are on the right track.你走在正确的轨道上。 Just further flatten the tupled values so the resulting table can be pivoted easily.只需进一步展平元组值,即可轻松旋转生成的表。
dic
is the given dict data. dic
是给定的 dict 数据。
df = pd.DataFrame(
[[k, v[0], v[1]] for k, ls_v in dic.items() for v in ls_v],
columns=["Person", "GUID", "value"]
).pivot(index="GUID", columns="Person")
# drop hierarchical level of "value"
df.columns = df.columns.droplevel(0)
print(df)
Person Person A Person B Person C
GUID
abc123 1.0 4.0 2.0
bcc222 2.0 NaN NaN
icy558 NaN NaN 7.0
igh643 1.0 NaN NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.