Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply two pandas series with mismatched indices

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

like image 253
intdt Avatar asked Jul 29 '15 19:07

intdt


People also ask

How do you multiply two Series in pandas?

multiply() function perform the multiplication of series and other, element-wise. The operation is equivalent to series * other , but with support to substitute a fill_value for missing data in one of the inputs.

Can pandas Series contain different data types?

Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.).

How do you compare pandas Series values?

It is possible to compare two pandas Series with help of Relational operators, we can easily compare the corresponding elements of two series at a time. The result will be displayed in form of True or False. And we can also use a function like Pandas Series. equals() to compare two pandas series.

How do you multiply all values in a data frame?

The mul() method multiplies each value in the DataFrame with a specified value. The specified value must be an object that can be multiplied with the values of the DataFrame.


1 Answers

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
like image 101
firelynx Avatar answered Sep 20 '22 17:09

firelynx