Multiply two pandas series with inconsistent indices

Two series were created: s1 and s2 from df .

Each of them has the same length, but different indices. s1.multiply(s2) combines inconsistent indices instead of multiplying them.

I just want to multiply entrywise s1 by s2 , ignoring inconsistent indexes.

I could run s1.reset_index() and s2.reset_index() and then take a column of these two dfs as it turns the original index into a separate column, but this is tedious, and I thought there might be an easier way to do it .

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

doesn't seem to work

+5
source share
1 answer

I think the transition from reset_index() is a way, but it is possible to drop the index, rather than returning it back to 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 support the reset_index() approach over the other proposed approach with simple multiplication by values

 s1 * s2.values 

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

This line says very clearly that you are solving the index problem:

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

Or split it into several lines:

 s1.reset_index(inplace=True, drop=True) s2.reset_index(inplace=True, drop=True) s1 * s2 
+1
source

All Articles