Thabra
Thabra

Reputation: 337

Transform pandas dataframe columns to list according to number in row

I have a dataframe like this:

Day            Id   Banana  Apple 
2020-01-01     1    1       1
2020-01-02     1    NaN     2
2020-01-03     2    2       2

How can I transform it to:

Day            Id   Banana  Apple  Products
2020-01-01     1    1       1      [Banana, Apple]
2020-01-02     1    NaN     2      [Apple, Apple]
2020-01-03     2    2       2      [Banana, Banana, Apple, Apple]

Upvotes: 3

Views: 56

Answers (1)

jezrael
jezrael

Reputation: 862911

Select all columns without first 2 by positions by DataFrame.iloc, then reshape by DataFrame.stack, repeat MultiIndex by Index.repeat and aggregate lists:

s = df.iloc[:, 2:].stack()
df['Products'] = s[s.index.repeat(s)].reset_index().groupby(['level_0'])['level_1'].agg(list)
print (df)
          Day  Id  Banana  Apple                        Products
0  2020-01-01   1     1.0      1                 [Banana, Apple]
1  2020-01-02   1     NaN      2                  [Apple, Apple]
2  2020-01-03   2     2.0      2  [Banana, Banana, Apple, Apple]

Or use custom function with repeat columns names without missing values:

def f(x):
    s = x.dropna()
    return s.index.repeat(s).tolist()

df['Products'] = df.iloc[:, 2:].apply(f, axis=1)
print (df)
          Day  Id  Banana  Apple                        Products
0  2020-01-01   1     1.0      1                 [Banana, Apple]
1  2020-01-02   1     NaN      2                  [Apple, Apple]
2  2020-01-03   2     2.0      2  [Banana, Banana, Apple, Apple]

Upvotes: 5

Related Questions