vendredi 5 juin 2015

Python pandas reference to filtered dataframe

Easier to explain/ask with an example...

I would like to filter df_1 below (>=4) as boolean, and for Trues, I would like df_result to take on the values in df_2. If that's unclear, here is how it should look...

df_1

.       'A'     'B'     'C'
0       0       10      9
1       9       7       4
2       9       3       6
3       10      3       7


df_2                    
                        
.       'A'     'B'     'C'
0       0.4     0.8     0.4
1       0.5     0.7     0.5
2       1.0     0.7     0.3
3       0.1     0.6     0.6
list = ['A','B','C']

for i in range(0,df_1.index.max):
    for j in list:
        if df_1.ix[i,j] >= 4 is True:
        df_result.ix[i,j] = df_2.ix[i,j]

The result should look like this...

df_result

.       'A'     'B'     'C'
0       Nan     0.8     0.4
1       0.5     0.7     0.5
2       1.0     NaN     0.3
3       0.1     NaN     0.6

...but alas, it's not happening yet. Can anyone help?

Thanks!

Aucun commentaire:

Enregistrer un commentaire