I have a dictionary that looks like this:
my_dict = {'product1' : [1, 511, 31, 222, 9020], 'product2' : [92, 511, 314, 4324], 'product3' : [40, 17, 4324]}
I want to create a dataframe that has the products as the index and the column names should be a range from 1 - 10 000, and if the value list associated to the product has the number it should be marked as a 1 or True in the column for the respective number.
1 2 3 ... 4324 .... 9020
product1 1 1
product2 1
product3 1
This entire dictionary has 80 products, each associated with a list of 0 to maybe a maximum of 50 numbers. The numbers could range between 1 and 10 000.
I would really appreciate any hints, I haven't managed to found a good way. I know I can create a dataframe from dictionary with pandas.DataFrame.from_dict() but it doesn't really do what I want.
I did a "ugly" solution, but I'm guessing this is not best practice or pythonic:
column_names = list(range(0,10000))
df = pd.DataFrame(columns = column_names)
for k, v in my_dict.items():
x = pd.Series(np.ones(len(v), dtype=int), index =v, name=k)
df = df.append(x)
print(df.head())
which outputs
0 1 2 3 4 5 6 7 8 9 ... \
product1 NaN 1.0 NaN NaN NaN NaN NaN NaN NaN NaN ...
product2 NaN NaN NaN 1.0 NaN NaN NaN NaN NaN NaN ...
product3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
9990 9991 9992 9993 9994 9995 9996 9997 9998 9999
product1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
product2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
product3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
[3 rows x 10000 columns]