Raja Hemanth
Raja Hemanth

Reputation: 11

How to concatenate certain columns with a condition of blank

I need to concatenate all the non-blank columns from a set of columns in a dataframe

my_df = pd.DataFrame({
    'Imp':  ['1', '2', '3'],
    'Apple':  ['a', 'b', 'c'],
    'Pear':   ['d',    ,    ],
    'Cherry': ['h',    , 'j']})

My desired output is the dataframe with the column 'Concat' in it

I only want to concatenate certain columns in my dataframe (Apple, Pear and Cherry)

Imp Apple  Pear Cherry  Concat
  1   a      d    h     a,d,h
  2   b                 b
  3   c           j     c,j

Upvotes: 1

Views: 78

Answers (1)

jezrael
jezrael

Reputation: 862681

If there are missing values:

my_df = pd.DataFrame({
    'Imp':  ['1', '2', '3'],
    'Apple':  ['a', 'b', 'c'],
    'Pear':   ['d',  np.nan  ,  np.nan  ],
    'Cherry': ['h',  np.nan  , 'j']})

my_df['new'] = my_df.iloc[:, 1:].apply(lambda x: ', '.join(x.dropna()), 1)
print (my_df)
  Imp Apple Pear Cherry      new
0   1     a    d      h  a, d, h
1   2     b  NaN    NaN        b
2   3     c  NaN      j     c, j

Or empty strings:

my_df = pd.DataFrame({
    'Imp':  ['1', '2', '3'],
    'Apple':  ['a', 'b', 'c'],
    'Pear':   ['d',  np.nan  ,  np.nan  ],
    'Cherry': ['h',  np.nan  , 'j']}).fillna('')

my_df['new'] = my_df.iloc[:, 1:].apply(lambda x: ', '.join(x[x != '']), 1)
print (my_df)
  Imp Apple Pear Cherry      new
0   1     a    d      h  a, d, h
1   2     b                    b
2   3     c           j     c, j

Upvotes: 2

Related Questions