lundi 22 février 2021

Conditionally replace values between dataframes

I have two pandas dataframes:

The first dataframe contains decimal values and has two rows and over 1000 columns; the rows are labelled 0 and 1.

The second has 100 row and again over 1000 columns, the values contained in the this dataframe are 0 and 1.

The columns in both dataframes are the same length and represent the same "feature".

I would like to bring these two dataframes together such that the 1's and 0's in the larger dataframe are replaced by the decimals in the first matrix.

To clarify lets look at a smaller example:

dataframe 1:

  |0| 0 | 1 | 2 |
  |0|0.2|0.3|0.5|
  |1|0.9|0.8|0.3|

dataframe 2:

  |0| 0 | 1 | 2 |
  |1| 0 | 1 | 0 |
  |2| 0 | 1 | 1 |
  |3| 0 | 1 | 0 |

I would like to create the following resulting matrix:

results: dataframe post algorithm:

 |0| 0   | 1   |  2  |
 |1| 0.2 | 0.8 | 0.5 |
 |2| 0.2 | 0.8 | 0.3 |
 |3| 0.2 | 0.8 | 0.5 |

My initial reaction was to make a copy of the second dataframe and iterate through this dataframe and replace the values accordingly...this seems time consuming though is there a better way?

The loop I will be using to iterate is something of the nature, but I not fully sure what it would look like...

   for i in df_data.itertuples(index=False, name=None):
        for k in enumerate(i):
          if df_data.iloc[i,k] ==1:
            adjust copy somehow...not sure yet...
          else:
        

Aucun commentaire:

Enregistrer un commentaire