'How to subset over indexes in pandas using lambdas
I have the following data sets
df1
f1 f2 f3
1 5 9
2 4 7
3 8 6
These values are indexes for a second df2
df2 - Expected result
index value1 value2 value3
1 0.2 0.2 0.1
2 0.5 0.7 0.8
3 0.4 0.2 0.1
4 0.3 0.1 0.3
5 0.1 0.4 0.1
Edit : I want to run a lambda function over each row of df1 to get a result like df2. As the number of rows and columns is very large, I don't want to use 'for each' type statements. Basically need an efficient way to iterate over df1.
Solution 1:[1]
You may check with replace after you get the columns and index match
df2 = df2.set_index('index')
df2.columns = df1.columns
out = df1.replace(df2)
Out[105]:
f1 f2 f3
0 0.2 0.4 9
1 0.5 0.1 7
2 0.4 8.0 6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BENY |
