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

firelynx
  • 30,616
  • 9
  • 91
  • 101
lebca
  • 185
  • 2
  • 5
  • you can convert to a numpy array which will ignore the index with `values`: `s1.values.mul(s2.values)`. – JohnE Jul 29 '15 at 19:16
  • 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) ) ? – lebca Jul 29 '15 at 19:27
  • 3
    `s1 * s2.values` should work – EdChum Jul 29 '15 at 19:44
  • 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 `s1` – JohnE Jul 29 '15 at 19:59
  • Ah, okay. Thank you both John and Ed. Both of those cover the solutions I needed. – lebca Jul 29 '15 at 21:29
  • Could someone add this an answer so we can close the question? – Julien Marrec Jul 30 '15 at 12:39

1 Answers1

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
firelynx
  • 30,616
  • 9
  • 91
  • 101