0

I have Series and DataFrames where the columns and indexes do not match. To operate on them I use something like

pd.DataFrame(df1.values + df2.values * s1.values, index = s1.index, columns = df1.columns)

The dimension follow NumPy broadcasting rules and the above works. I am looking for a better way to achieve the above which would be memory efficient and fast.

NB: The index and columns are inherited from of the Pandas Object.

Sometimes the indices are from the same Object:

pd.DataFrame(df1.values + df2.values * s1.values, index = df1.index, columns = df1.columns)

Sample data:

import pandas as pd

df1 = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10]], index=[1, 2, 3, 4], columns=['A', 'B', 'C', 'D'])

df2 = pd.DataFrame([[10, 9, 8, 7], [8, 7, 6, 5], [6, 5, 4, 3], [4, 3, 2, 1]], index=['R1', 'R2', 'R3', 'R4'], columns=['C1', 'C2', 'C3', 'C4'])

res1 = pd.DataFrame(df1.values + df2.values, index = df1.index, columns = df1.columns)

res2 = pd.DataFrame(df1.values + df2.values, index = df1.index, columns = df2.columns)

s1 = pd.Series([1, 2, 3, 4], index=['I1', 'I2', 'I3', 'I4'])

res3 = pd.DataFrame(df1.values + df2.values * s1.values, index = s1.index, columns = df2.columns)

res4 = pd.DataFrame(df1.values + df2.values * s1.values, index = df1.index, columns = df1.columns)

Expected result:

>>> res1
    A   B   C   D
1  11  11  11  11
2  11  11  11  11
3  11  11  11  11
4  11  11  11  11
>>> res2
   C1  C2  C3  C4
1  11  11  11  11
2  11  11  11  11
3  11  11  11  11
4  11  11  11  11
>>> res3
    C1  C2  C3  C4
I1  11  20  27  32
I2  11  18  23  26
I3  11  16  19  20
I4  11  14  15  14
>>> res4
    A   B   C   D
1  11  20  27  32
2  11  18  23  26
3  11  16  19  20
4  11  14  15  14

Undesirable results when using shorter expression:

df1 + df2
     A   B   C  C1  C2  C3  C4   D
1  NaN NaN NaN NaN NaN NaN NaN NaN
2  NaN NaN NaN NaN NaN NaN NaN NaN
3  NaN NaN NaN NaN NaN NaN NaN NaN
4  NaN NaN NaN NaN NaN NaN NaN NaN
R1 NaN NaN NaN NaN NaN NaN NaN NaN
R2 NaN NaN NaN NaN NaN NaN NaN NaN
R3 NaN NaN NaN NaN NaN NaN NaN NaN
R4 NaN NaN NaN NaN NaN NaN NaN NaN

df1 + df2 * s1
     A   B   C  C1  C2  C3  C4   D  I1  I2  I3  I4
1  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
R1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
R2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
R3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
R4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
  • Please provide a small but reproducible data set and desired data set. Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your question accordingly. – MaxU - stand with Ukraine Apr 14 '17 at 08:37
  • Why are you not satisfied with the way you are currently doing things? – IanS Apr 14 '17 at 09:40
  • I want to know if there is a way to use pandas DataFrame for element wise operations where the indices are taken from one of the DataFrames or a way to shorten an expression like `res4 = pd.DataFrame(df1.values + df2.values * s1.values, index = df1.index, columns = df1.columns)`. This is very long and the formula is lost in the boiler plate. When there are multiple formulas and longer expressions it becomes unclear what the formula is and things do not fit nicely into one line. – Suminda Sirinath S. Dharmasena Apr 14 '17 at 09:48
  • You method should be fast. If you want to clean up your code, assign to shorter variable names – piRSquared Apr 14 '17 at 11:15

0 Answers0