I have a very sparse dataframe df
such as this:
Apples Bananas Pineapple Mango
Mary Apples NaN NaN NaN
Jane NaN Bananas NaN NaN
Diego NaN NaN NaN Mango
Guido NaN NaN Pineapple NaN
and I would like to build a dictionary d
such as
d = {'Apples':3, 'Bananas':1, 'Pineapple':2, 'Mango': 15}
to obtain
Apples Bananas Pineapple Mango
Mary 3 NaN NaN NaN
Jane NaN 1 NaN NaN
Diego NaN NaN NaN 15
Guido NaN NaN 2 NaN
I can do
df.to_sparse().replace(d)
but it's been over 30' and no output yet. My dataframe has 10000 rows times 1500 columns, the dataframe is originally 135MB, that becomes 850kB after to_sparse(). Is there any faster way?