Create new column with Pandas .apply() even if DataFrame is empty

Viewed 387

I want to use Pandas apply to create a new column, and I want this functionality to be fail-save even if that DataFrame is empty. Here is a minimal example that works as expected:

df      = pd.DataFrame(np.array([[1,2],[3,4]]), columns=['a','b']) # two columns
add     = lambda x: x['a'] + x['b'] # add column a and b # add two values
df['c'] = df.apply( add, axis=1 ) # creates new column c, as anticipated

However, it gets problematic when df happens to be empty. Consider the following example where now the DataFrame is empty, but otherwise equal:

df      = pd.DataFrame( columns=['a','b']) # two columns, but no values
df['c'] = df.apply( add, axis=1 ) # raises an error!

How can I execute this last column safely, such that it just appends a column 'c' to the DataFrame, even if df is empty?

Interestingly enough, this works

df.apply( add, axis=1 )

but cannot be appended as column 'c'.

1 Answers

If you want to create a new column c based on the sum of columns a and b, then you can just do the following:

df['c'] = df['a'] + df['b']  # creates new column c, as anticipated :)

That way, you don't need to assign a lambda expression to a function add, (it is not recommended to assign lambda expressions a function).

import numpy as np
import pandas as pd

df = pd.DataFrame(np.array([[1, 2], [3, 4]]), columns=['a', 'b'])  # two columns
print(df)
   a  b
0  1  2
1  3  4

df['c'] = df['a'] + df['b']  # creates new column c, as anticipated
print(df)
   a  b  c
0  1  2  3
1  3  4  7

df = pd.DataFrame(columns=['a', 'b'])  # two columns, but no values
df['c'] = df['a'] + df['b']  # creates new column c, as anticipated
print(df)
Empty DataFrame
Columns: [a, b, c]
Index: []

Even if the dataframe is empty, above method will work.

Related