简体   繁体   中英

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)), ...)

{'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). 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.

Code

dic is the given dict data.

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)

Result

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM