New column as list from other columns, but without nans

Viewed 83

Basically, I have dataframe like this:

  c1   c2    
0  a    x  
1  b  NaN  

and I want to have column c like this:

  c1   c2       c
0  a    x  [a, x]
1  b  NaN     [b]

Here is my solution:

import pandas as pd
import numpy as np

df = pd.DataFrame({'c1': ['a', 'b'], 'c2': ['x', np.nan]})

df['c'] = df[['c1', 'c2']].values.tolist()
df['c'] = df['c'].apply(lambda x: [i for i in x if i is not np.nan])

but I suppose something shorter, simpler and more pandonic exists. Could you help me with one-liner for this?

2 Answers
df["c"] = df.apply(lambda x: x[x.notna()].tolist(), axis=1)
print(df)

Prints:

  c1   c2       c
0  a    x  [a, x]
1  b  NaN     [b]

Let us try with stack and groupby

df['c'] = df.stack().groupby(level=0).agg(list)

Alternate approach with list comprehension

df['c'] = [v[pd.notna(v)] for v in df.values]

  c1   c2       c
0  a    x  [a, x]
1  b  NaN     [b]
Related