0

I have a result data frame with only indices to be populated with results that comes through on a row by row operation.

       a   res1   res2   res3  
    0  1
    1  4  
    2  0 
    3  1  
    4  5  

I'm looping through each row with

    df.apply(func, axis=1) 

which points to another function coming up with results for each row. The results are calculated from other dfs and I basically do a linear combination of several rows depending on the index column a like so:

    def func(row):
        ...
        combinedrow = x*df2.iloc[index1]+y*df3.iloc[index2] + etc..

Each of the other dfs have the same results headings. I want to basically insert this row result into each row like this:

       a  res1  res2   res3
    0  1   2      3      4
    1  4  
    2  0 
    3  1  
    4  5           

it has to be done one row at a time because of the apply() function.

How do i do the partial row merge? Performance is very important, hence i've not inserted each number separately under the columns.

Dave Smith
  • 11
  • 2
  • 2
    Welcome to SO. Please review [ask] and create a [mcve]. It is unclear what you are trying to do. – user3483203 Feb 05 '20 at 18:06
  • 2
    Could you show that other function or the values ​​to be inserted and its format? From this depends the solution to your problem – ansev Feb 05 '20 at 18:10
  • In general it's slow to use loop. You can use `.apply(...,axis=1)` to apply the function to each row without looping. Then, to add the results back to the original dataframe, see: https://stackoverflow.com/questions/20602947/append-column-to-pandas-dataframe – Tim Feb 05 '20 at 18:11
  • What is the issue, exactly? Have you tried anything, done any research? _I have a data frame with only indices_ That DataFrame looks like it has a column though, no? _Performance is very important, hence i have not inserted each value by column name._ What do you mean? I think we're going to need to see more of your program. – AMC Feb 05 '20 at 18:46
  • ansev, i have just updated the question. if anything else is unclear please point out. thanks – Dave Smith Feb 05 '20 at 19:50

0 Answers0