Home > OS >  python dataframe combining columns of lists
python dataframe combining columns of lists

Time:06-15

Basically I want to combine dataframe columns with lists, and keep it as a list.

df
     a                      b
['2%', '70%']    ['2% W/V', '70% V/V']
['100%']         []
['5%', '60%']    ['5% W/V', '60% V/V']

And I want to have:

df
        c 
['2% W/V', '70% V/V']
['100%']         
['5% W/V', '60% V/V']

I can do this by setting df['b'] column to a str and use combine_first:

df = df.astype({"b": str}).copy()
df['b'] = df['b'].replace('[]', np.nan)
df['c'] = df['b'].combine_first(df['a']).copy()

But I want to keep it as a list so I can combine it with other columns of lists. I tried using:

df['c'] = df['b'].combine_first(df['a']).copy()

Without setting df['b'] to a str but it did not work

CodePudding user response:

combine_first function doesn't work with empty list type.

    df['b'] = df['b'].apply(lambda x: x if x else np.nan)
    df['c'] = df['b'].combine_first(df['a'])

It works without setting to str type.

  • Related