Reputation: 337
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
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 list
s:
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