Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 449
Thabra Avatar asked Jan 23 '26 15:01

Thabra


1 Answers

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]
like image 176
jezrael Avatar answered Jan 26 '26 06:01

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!