One solution is to use pd.concat in combination with result_type='expand'.

cols = {0: 'r1', 1: 'r2', 2: 'r3'}
df = pd.concat([df, df.apply(my_function, axis=1, result_type='expand')], axis=1).rename(columns=cols)

You have to rename the columns afterwards. Also, the resulting empty dataframe repeats the first two columns:

Output:

    foo bar foo bar

versus

    foo bar

Both dataframes are empty, so it may be of no interest for you.


I think to check for empty dataframes in pandas is good practice. So, Siddhants solution in the comment is just fine.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top