Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Dataframe yes/no checker

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
like image 613
ajd018 Avatar asked Dec 13 '22 07:12

ajd018


2 Answers

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
like image 137
Andrej Kesely Avatar answered Dec 26 '22 06:12

Andrej Kesely


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
like image 34
Corralien Avatar answered Dec 26 '22 06:12

Corralien