简体   繁体   English

如果值使用pandas落在另一个数据框的范围内,则从另一个数据框添加列

[英]add column from another data frame if the value falls under the range from the other data frame using pandas

I have a data frame like this: 我有一个像这样的数据框:

df1
col1     col2      col3
  A       11        RS
  B       23        PN
  A       24        LR
  C       23        TN
  D       1         WB
  C       23        PR

another data frame: 另一个数据框:

df2
name     min     max   points
 A        1      15       1
 A        15     30       2
 B        1      15       1
 B        15     30       2
 C        1      15       1
 C        15     30       2
 D        1      15       1
 D        15     30       2

I want to put points values from df2 to df1 if the col2 values of df1 falls under the max value and min values of df2 如果df1的col2值低于df2的最大值和最小值,我想将点值从df2放到df1

The data frame I am looking for 我正在寻找的数据框

df3
col1     col2      col3     points
  A       11        RS         1
  B       23        PN         2
  A       24        LR         2
  C       23        TN         2
  D       1         WB         1
  C       23        PR         2

How to do it in most efficient using pandas 如何使用熊猫最有效地做到这一点

Use merge first and then filter by boolean indexing with between : 首先使用merge ,然后使用betweenboolean indexing进行过滤:

df = df1.merge(df2, left_on='col1', right_on='name')
df = df[df['col2'].between(df['min'], df['max'])].drop(['name','min','max'], axis=1)
print (df)
   col1  col2 col3  points
0     A    11   RS       1
3     A    24   LR       2
5     B    23   PN       2
7     C    23   TN       2
9     C    23   PR       2
10    D     1   WB       1

Solution for add column points : 添加列points解决方案:

df = df1.reset_index().merge(df2, left_on='col1', right_on='name')
df = df.loc[df['col2'].between(df['min'], df['max']),['index','points']]
print (df)
    index  points
0       0       1
3       2       2
5       1       2
7       3       2
9       5       2
10      4       1

df1['points'] = df.set_index('index')['points']
print (df1)
  col1  col2 col3  points
0    A    11   RS       1
1    B    23   PN       2
2    A    24   LR       2
3    C    23   TN       2
4    D     1   WB       1
5    C    23   PR       2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从某个范围内的列中找到一个固定值,并在熊猫数据框中找到另一列的每个唯一值 - find a fix value from a column around a range with each unique values of another column in pandas data frame Pandas:根据另一个数据框列中的值范围计算单独数据框列框中的值(python) - Pandas: Calculating a value in a separate data frame column frame based on range of values in another data frame column (python) pandas:从一个数据帧添加行到另一个数据帧? - pandas: Add row from one data frame to another data frame? Pandas - 从数据框中的列中提取值 - Pandas - Extract value from column in data frame 使用来自另一个数据帧的比率分解 pandas 数据帧 - Disaggregate pandas data frame using ratios from another data frame 在Pandas数据帧中确定值落入的分位数 - determine the quantile a value falls in, in a Pandas data frame 从数据框中提取相对于其他数据框中的bin值的行(不使用列名) - Extracting rows from a data frame with respect to the bin value from other data frame(without using column names) 为熊猫数据框的单独列(来自特定列范围)的最大值选择相应的列值 - Select corresponding column value for max value of separate column(from a specific range of column) of pandas data frame 使用来自另一个数据帧的时间索引插入 pandas 帧 - interpolate pandas frame using time index from another data frame 根据另一个范围从熊猫数据框中选择行 - Select rows from pandas data frame based on range from another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM