1

I am trying to concatenate multiple columns(all are numeric number) in a dataframe into an array under one single column of a dataframe. Caveat is if the concatenating columns have NAN, then should not be concatenated.

Input dataframe:

userid | p1 | p2 |p3 | p4 | p5
 1     | NAN| NAN| 20| 30 | 40
 1     | NAN| 30 | 60| 80 | 100
 2     | NAN| NAN|NAN| NAN| 45

I have tried this solution but it doesnt drop the nan:

df['combined'] = df[['p5','p4','p3','p2','p1']].apply(tuple,axis=1).apply(np.array)

Final Output dataframe must look like this(also the order of concatenation is p5,p4,p3,p2,p1) while also excluding nan while concatenating:

userid | p1 | p2 |p3 | p4 | p5 | combined
 1     | NAN| NAN| 20| 30 | 40 | [40,30,20]
 1     | NAN| 30 | 60| 80 | 100| [100,80,60,30]
 2     | NAN| NAN|NAN| NAN| 45 | [45]

So any solution for the above output will be really appreciated.

1 Answer 1

1

You could use pd.isna to filter out the NaN values with boolean mask:

df['combined'] = df[['p5', 'p4', 'p3', 'p2', 'p1']].apply(lambda x: x[~pd.isna(x)].values, axis=1)
print(df)

Output

   userid  p1    p2    p3    p4   p5                   combined
0       1 NaN   NaN  20.0  30.0   40         [40.0, 30.0, 20.0]
1       1 NaN  30.0  60.0  80.0  100  [100.0, 80.0, 60.0, 30.0]
2       2 NaN   NaN   NaN   NaN   45                     [45.0]

As mentioned by @jpp, you could also drop them, by using dropna:

lambda x: x.dropna().values
Sign up to request clarification or add additional context in comments.

3 Comments

Or df.iloc[:, 1:].apply(lambda x: x.dropna().tolist(), axis=1) ?
@Daniel - Thanks you for your solution provided, this worked for me
@Datta glad I could help. Please consider marking this answer as accepted as it will signal others that your problem was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.