I have a dataframe with results as below. Sample dataframe shown actual one is much larger. I want to get a dictionary (or another structure if it will be faster) with the
You can use .apply
.apply
df.apply(lambda x: list(x.dropna().index), axis=1).to_dict() #Updated answer # Or dict(df.apply(lambda x: list(x.index[~x.isnull()]), axis=1)) #Original answer
Output:
{1: ['MSFT'], 2: ['GOOG', 'AMZN'], 3: ['AAPL', 'AMZN', 'FB'], 4: ['FB']}