简体   繁体   English

根据条件从另一个数据帧的值向数据帧添加新列

[英]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.

相关问题 根据另一个数据帧的列值的条件将数据添加到数据帧中的列 - Adding data to columns in a dataframe based on condition on column values of another dataframe 根据条件从另一个 Dataframe 添加新数据到 Dataframe - Adding new data to a Dataframe from another Dataframe based on condition 根据另一个 dataframe 的值向 dataframe 添加新列 - Adding a new column to a dataframe based on the values of another dataframe 根据另一个数据框中的值将列添加到数据框中 - Adding column to dataframe based on values in another dataframe 根据条件从另一个 dataframe 值替换列的值 - Python - Replace values of a column from another dataframe values based on a condition - Python Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe Pandas 数据框根据另一列的条件创建新行 - Pandas dataframe create new rows based on condition from another column 根据列条件从另一个 dataframe 添加值的列 - Adding a column with values from another dataframe based on column conditions 根据条件将一个 dataframe 列的值分配给另一个 dataframe 列 - assign values of one dataframe column to another dataframe column based on condition 基于另一个 dataframe 列在 dataframe 中添加新列 - adding a new column in a dataframe based on another dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM