I would like to make a table that evaluates whether a user is in a group or not. How can I get my dictionary sorted like in the example I have down below? I would like the index and columns populated automatically by the key and value.
d = {
'user1': ['group1', 'group2', 'group3'],
'user2': ['group1', 'group2'],
'user3': ['group2']}
df = pd.DataFrame.from_dict(d,orient='index')
print(df)
Current result
user1 user2 user3
0 group1 group1 group2
1 group2 group2 NaN
2 group3 NaN NaN
Desired result - I would like the rows based on the key and the columns based on the values.
group1 group2 group3
user1 Y Y Y
user2 Y Y N
user3 N Y N
Try:
d = {
"user1": ["group1", "group2", "group3"],
"user2": ["group1", "group2"],
"user3": ["group2"],
}
df = pd.DataFrame.from_dict(d, orient="index")
x = df.stack().droplevel(level=1)
x = pd.crosstab(x.index, x).replace({1: "Y", 0: "N"})
x.index.name, x.columns.name = None, None
print(x)
Prints:
group1 group2 group3
user1 Y Y Y
user2 Y Y N
user3 N Y N
Convert your dict to a list of tuples with a comprehension:
>>> pd.DataFrame([(u, g, 'Y') for u, lg in d.items() for g in lg]) \
.pivot(0, 1, 2).fillna('N').rename_axis(index=None, columns=None)
group1 group2 group3
user1 Y Y Y
user2 Y Y N
user3 N Y N
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With