Transform pandas dataframe columns to list according to number in row

Thabra :

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]
jezrael :

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]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=361179&siteId=1