Home > Software design >  Combine Value in Multiple Columns (With NA condition) Into New Column
Combine Value in Multiple Columns (With NA condition) Into New Column

Time:07-18

I have data:

ID  merk_ac1  merk_ac2 merk_ac3
1   a         NA       NA
2   NA        b        NA
3   NA        a        b
4   a,b       NA       c

I tried using combine_first() but not cover the multiple values. I want the result like:

ID  merk_ac1  merk_ac2 merk_ac3 merk_ac
1   a         a                 a 
2             b                 b
3             a        b        a,b
4   a,b                c        a,b,c

CodePudding user response:

What about something like:

import numpy as np
import panas as pd

df['merk_ac'] = df.apply(lambda x: [element for element in x if ~np.isnan(element)],axis = 1)

assuming that in merk_ac you want a list with all the elements from the different columns of the original data frame

CodePudding user response:

You can use apply to concatenate string from multiple columns using join

import pandas as pd

a = ["a", pd.NA, pd.NA, "a,b"]
b = [pd.NA, "b", "a", pd.NA]
c = [pd.NA, pd.NA, "b", "c"]

df = pd.DataFrame({"a": a, "b": b, "c": c})
df["combined"]=df.apply(lambda row: ",".join([elem for elem in row if not pd.isna(elem)]), axis=1)
df.head()

OUTPUT:

a b c combined
0 a NA NA a
1 NA b NA b
2 NA a b a,b
3 a,b NA c a,b,c
  • Related