Saturday, 15 December 2018

How do I operate on a DataFrame with a Series for every column

Objective and Motivation

I've seen this kind of question several times over and have seen many other questions that involve some element of this. Most recently, I had to spend a bit of time explaining this concept in comments while looking for an appropriate canonical Q&A. I did not find one and so I thought I'd write one.

This question usually arises with respect to a specific operation but equally applies to most arithmetic operations.

  • How do I subtract a Series from every column in a DataFrame?
  • How do I add a Series from every column in a DataFrame?
  • How do I multiply a Series from every column in a DataFrame?
  • How do I divide a Series from every column in a DataFrame?

The Question

Given a Series s and DataFrame df. How do I operate on each column of df with s?

df = pd.DataFrame(
    [[1, 2, 3], [4, 5, 6]],
    index=[0, 1],
    columns=['a', 'b', 'c']
)

s = pd.Series([3, 14], index=[0, 1])

When I attempt to add them, I get all np.nan

df + s

    a   b   c   0   1
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN

What I thought I should get is

    a   b   c
0   4   5   6
1  18  19  20



from How do I operate on a DataFrame with a Series for every column

No comments:

Post a Comment