append column to another list-like column in same dataframe

Viewed 38

Consider following dataframe and the need to merge A and B in an expanded list:

df = pd.DataFrame([[['abc', 'def'], 'zzz']] * 3, columns=['A', 'B'])

            A    B
0  [abc, def]  zzz
1  [abc, def]  zzz
2  [abc, def]  zzz

add (or +) won't work as-is because B is a string so I am doing so:

df.A = df.A.add(df.B.apply(lambda x: [x]))
df.drop(['B'], axis=1)

                 A
0  [abc, def, zzz]
1  [abc, def, zzz]
2  [abc, def, zzz]

but I was wondering if there is a better (performing) way.

I could not find a one-liner built-in method to add values of a Series to an existing list-like Series. It seems you always must apply() to a list first.

4 Answers

Here is a one liner:

df.apply(lambda x: x['A'] + [x['B']], axis=1).to_frame('A')

@Yannick,

You could do this:

   import pandas as pd

   df = pd.DataFrame([[['abc', 'def'], ['zzz']]] * 3, columns=['A', 'B'])
   df['C'] = df['A'] + df['B'] 
   print(df)

You may use the help from numpy reshape

df.A.add(df.B.values[:,None].tolist())

Out[81]:
0    [abc, def, zzz]
1    [abc, def, zzz]
2    [abc, def, zzz]
dtype: object

For the record, here some time testing based on answers so far and this related post

import pandas as pd
df = pd.DataFrame([[['abc', 'def'], 'zzz']] * 2**20, columns=['A', 'B'])

%timeit df.A.add(df.B.apply(lambda x: [x]))
%timeit df.A.add(df.B.map(lambda x: [x]))
%timeit df.A.add(df.B.values[:,None].tolist())
%timeit pd.Series([r[0] + [r[1]] for r in df.values.tolist()])

1 loop, best of 3: 321 ms per loop
1 loop, best of 3: 374 ms per loop
1 loop, best of 3: 313 ms per loop
1 loop, best of 3: 476 ms per loop

So not much differences.

Related