[英]Adding a new column to a dataframe from the values of another dataframe based on a condition
I have two different DataFrames of different sizes.我有两个不同大小的不同 DataFrame。
a = np.arange(0,100,10)
b = np.random.random_sample((10,))
df1 = pd.DataFrame({'a': a, 'b': b})
df1
a b
0 0 0.340319
1 10 0.821281
2 20 0.592853
3 30 0.589032
4 40 0.533543
5 50 0.628839
6 60 0.431976
7 70 0.306126
8 80 0.080576
9 90 0.533240
c = np.arange(4,14,2)
df2 = pd.DataFrame({'c': c})
df2
c
0 4
1 6
2 8
3 10
4 12
By comparing the values in column 'c' in df2 to the values in column 'a' in df1, I'd like to add df2 a new column, which'll consist of the values in column 'b' of df1.通过将 df2 中“c”列中的值与 df1 中“a”列中的值进行比较,我想向 df2 添加一个新列,该列将由 df1 中“b”列中的值组成。
For example, the first three values of column c are 4, 6, and 8, which are all in the range defined by the first two rows of column a in df1 (0 to 10).比如c列的前三个值是4、6、8,都在df1中a列的前两行定义的范围内(0到10)。 That's why in the new column created, I'd like assign the b value of row a = 0 (0.340139) to all of them.
这就是为什么在创建的新列中,我想将 a = 0 (0.340139) 行的 b 值分配给所有这些。
Similarly, for 10 and 12 in column c, they should get the b value of row a=10 (0.821281), since they are in between 10 and 20.类似地,对于 c 列中的 10 和 12,它们应该得到行 a=10 (0.821281) 的 b 值,因为它们在 10 和 20 之间。
So at the end, I should end up with a DataFrame like this.所以最后,我应该得到一个像这样的 DataFrame。
df2
c d
0 4 0.340319
1 6 0.340319
2 8 0.340319
3 10 0.340319
4 12 0.821281
So if you guys have any tips on that, it'd be greatly appreciated.因此,如果你们对此有任何提示,将不胜感激。
We can try merge_asof
我们可以试试
merge_asof
out = pd.merge_asof(df2, df1, left_on='c',right_on='a', allow_exact_matches=False)
c a b
0 4 0 0.340319
1 6 0 0.340319
2 8 0 0.340319
3 10 0 0.340319
4 12 10 0.821281
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.