0

I'm searching to improve my code, and I don't find any clue for my problem. I have 2 dataframes (let's say A and B) with same number of row & columns.

I want to create a third dataframe C, which will transformed each A[x,y] element based on B[x,y] element.

Actually I perform the operation with 2 loop, one for x and one for y dimension :

import pandas

A=pandas.DataFrame([["dataA1","dataA2","dataA3"],["dataA4","dataA5","dataA6"]])
B=pandas.DataFrame([["dataB1","dataB2","dataB3"],["dataB4","dataB5","dataB6"]])
Result=pandas.DataFrame([["","",""],["","",""]])

def mycomplexfunction(x,y):
    return str.upper(x)+str.lower(y)

for indexLine in range(0,2):
    for indexColumn in range(0,3):
        Result.loc[indexLine,indexColumn]=mycomplexfunction(A.loc[indexLine,indexColumn],B.loc[indexLine,indexColumn])

print(Result)

              0             1             2
0  DATAA1datab1  DATAA2datab2  DATAA3datab3
1  DATAA4datab4  DATAA5datab5  DATAA6datab6

but I'm searching for a more elegant and fastway to do it directly by using dataframe functions.

Any idea ? Thanks,

Machin
  • 41
  • 5
  • 1
    post raw data, code for both dfs, your attempts, and desired output otherwise this becomes a fishing expedition – EdChum Mar 16 '17 at 12:06
  • Right I edited my question – Machin Mar 16 '17 at 13:42
  • If you need to iterate over rows in a dataframe always use a builtin iterator like `iterrows()` or `itertuples()`, normal `for`-loops are horribly slow, see e.g. [here](http://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas). – Khris Mar 16 '17 at 14:13
  • I did the change : really fastest !!! Thank you – Machin Mar 16 '17 at 15:54

0 Answers0