I have a list of transactions/tuples in Python with varying number or elements, like this:
lst = [('apple','banana','carrots'),('apple',),('banana','carrots',)]
I would like to store this list in a tabular form (preferably in a pd.DataFrame
) such as this:
apple banana carrots
0 1 1 1
1 1 0 0
2 0 1 1
But if try to convert directly using pd.DataFrame
, I get his instead:
pd.DataFrame(lst)
0 1 2
0 apple banana carrots
1 apple None None
2 banana carrots None
How can I convert this type of list into a binary table?
This is very simple if you use value_counts
over columns i.e
pd.DataFrame(lst).apply(pd.value_counts,1).fillna(0)
apple banana carrots
0 1.0 1.0 1.0
1 1.0 0.0 0.0
2 0.0 1.0 1.0
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