Search code examples
pythonpandasdataframeconcatenation

What the best possible way to concatenate pandas column and count ? From a list of column


I have dataframe like this:

A     B     C    D     E      F
aa    bb    cc  dd    ee      ff
NA    ba    NA  da    ea      NA

list_col = ['A', 'B', 'C']

So i just want to merge the columns which are in list only. Moreover i dont want NA values as merged.. is there any way? and the i need one more column which give the count (How many columns are merged as you can see in my desired output

I can calculate the "desired_col" by this:

df['desired_col'] = df[list_col].apply(lambda x: '-'.join(x.dropna()), axis=1)

desired_output

 A     B     C    D     E      F         desired_col       desired_count
aa    bb    cc  dd    ee      ff          aa-bb-cc            3
NA    ba    NA  da    ea      NA             ba               1

Solution

  • another solution, the concatenation section is long though

    df['desired_col'] = df.filter(list_col).fillna('').add('-').sum(axis=1).str.strip('-')
    df['count'] = df.desired_col.str.split('-').str.len()
    df
    
         A  B   C   D   E   F   desired_col count
    0   aa  bb  cc  dd  ee  ff  aa-bb-cc    3
    1   NaN ba  NaN da  ea  NaN ba          1