Multiply two pandas series with mismatched indices
Asked Answered
F

1

9

Created two series: s1 and s2 from df.

Each have same length but differing indices. s1.multiply(s2) unions the mismatched indices instead of multiplying against them.

I just want to multiply entrywise s1 against s2 ignoring the mismatched indices.

I could run s1.reset_index() and s2.reset_index() and then take the column I want from these two dfs, since it turns the original index into a separate column, but that's tedious and I thought there might be a simpler way to do it.

s1.multiply(s2, axis='columns')

doesn't seem to work either

Fortran answered 29/7, 2015 at 19:0 Comment(6)
you can convert to a numpy array which will ignore the index with values: s1.values.mul(s2.values).Pietra
Thanks John, that does indeed work to multiply the values of the series. Unfortunately, it converts the series to a numpy array. Do you know of a way to keep the whole process using series, instead of moving to numpy arrays, and then back to series ( result = pandas.Series(s1.values*s2.values) ) ?Fortran
s1 * s2.values should workSamul
It depends on what you want the index to be. Your suggestion will result in a fresh [0,1,2...] index whereas Ed's suggestion will use the index from s1Pietra
Ah, okay. Thank you both John and Ed. Both of those cover the solutions I needed.Fortran
Could someone add this an answer so we can close the question?Silicon
B
4

I think going with reset_index() is the way, but there is an option to drop the index, not push it back into the dataframe.

Like this:

s1 = pd.Series([1,2,3,4,5,6,7], index=[52,34,3,53,636,7,4])
52     1
34     2
3      3
53     4
636    5
7      6
4      7
dtype: int64

s1.reset_index(drop=True)
0    1
1    2
2    3
3    4
4    5
5    6
6    7
dtype: int64

The reason I favour the reset_index() approach before the other suggested approach with simply multiplying by values

s1 * s2.values

is that this is not very explicit. This line does not tell me that there is an index problem that you are solving.

While this line tells the story very explicitly that you are solving an index problem:

s1.reset_index(drop=True) * s2.reset_index(drop=True)

Or break it down to multiple rows:

s1.reset_index(inplace=True, drop=True)
s2.reset_index(inplace=True, drop=True)
s1 * s2
Balzer answered 30/7, 2015 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.