Home > Software engineering >  dummies-like dataframe from nested list of keys
dummies-like dataframe from nested list of keys

Time:01-28

How to convert following list to a pandas dataframe?

my_list = [["A","B","C"],["A","B","D"]]

And as an output I would like to have a dataframe like:

Index A B C D
1 1 1 1 0
2 1 1 0 1

CodePudding user response:

You can craft Series and concatenate them:

my_list = [["A","B","C"],["A","B","D"]]

df = (pd.concat([pd.Series(1, index=l, name=i 1)
                for i,l in enumerate(my_list)], axis=1)
        .T
        .fillna(0, downcast='infer') # optional
      )

or with get_dummies:

df = pd.get_dummies(pd.DataFrame(my_list))
df = df.groupby(df.columns.str.split('_', 1).str[-1], axis=1).max()

output:

   A  B  C  D
1  1  1  1  0
2  1  1  0  1

CodePudding user response:

I'm unsure how those two structures relate. The my_list is a list of two lists containing ["A","B","C"] and ["A", "B","D"].

If you want a data frame like the table you have, I would suggest making a dictionary of the values first, then converting it into a pandas dataframe.

my_dict = {"A":[1,1], "B":[1,1], "C": [1,0], "D":[0,1]}
my_df = pd.DataFrame(my_dict)
print(my_df)

Output

  •  Tags:  
  • Related