简体   繁体   English

根据另一个数据框查找列值

[英]find column value based on another dataframe

I've a sample dataframe我有一个示例数据框

value    id
  a       1
  b       2
  c       5
  d       8
  e       11

another dataframe:另一个数据框:

entity     start_range       end_range
 ABC           1                 3
 DEF           4                 7
 XYZ           8                 15

How can I get the values of entities in dataframe1 based on range which would look like the below?如何根据如下所示的范围获取 dataframe1 中实体的值?

value    id       entity
  a       1        ABC
  b       2        ABC
  c       5        DEF
  d       8        XYZ
  e       11       XYZ

it's not a clean answer and I don't know if there is a better way to do this but try this it should works:这不是一个干净的答案,我不知道是否有更好的方法来做到这一点,但试试这个它应该有效:

data=pd.DataFrame({"value":["a","b","c","d","e"],"id":[1,2,5,8,11]})
df=pd.DataFrame({"entity":["ABC","DEF","XYZ"],"start_range":[1,4,8],"end_range":[3,7,15]})
df["explode"]=df.apply(lambda x:[i for i in range(x["start_range"],x["end_range"])],axis=1)
exploded=df.explode("explode")
exploded.index=exploded["explode"]
data["entity"]=data["id"].replace(exploded["entity"].to_dict())

You can do:你可以做:

ii = pd.IntervalIndex.from_arrays(df2['start_range'], df2['end_range'], closed='both')
df1['entity'] = df2.set_index(ii).loc[df1['id'], 'entity'].values

print(df1)

  value  id entity
0     a   1    ABC
1     b   2    ABC
2     c   5    DEF
3     d   8    XYZ
4     e  11    XYZ

So the problem to be solved in this case is how to search for 'id' in the range.所以本例要解决的问题是如何在范围内搜索'id'。 Apparently loc can do that !显然loc可以做到这一点! as long as your index is IntervalIndex .只要您的索引是IntervalIndex So create IntervalIndex from df2 and use that to loc the df1's id所以从 df2 创建 IntervalIndex 并使用它来locid

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

相关问题 根据具有共享列的另一个数据帧在数据帧中查找第一次出现的值 - Find first occurrence of value in dataframe based on another dataframe with a shared column 如何根据数据框的另一列中的条件查找列中的最小值? - How to find minimum value in a column based on condition in an another column of a dataframe? 基于另一个列值查找最小值并合并到 1 个数据帧的循环? - A loop to find min values based on another column value, and merge into 1 dataframe? Pyspark DataFrame 列基于另一个 DataFrame 值 - Pyspark DataFrame column based on another DataFrame value 根据来自另一个数据帧的列值在数据帧中查找一行并对其应用过滤器 - Find a row in a dataframe based on a column value from another dataframe and apply filter to it 根据另一列中的值组合数据框的列 - Combining columns of dataframe based on value in another column 在另一列中查找列负值 - dataframe - Find columns negative value in another column - dataframe 根据条件使用另一个数据帧列值更新一个数据帧值 - Update one dataframe value with another dataframe column value based on the condition 根据来自另一个 dataframe 列的值选择列值 - Selecting a column value based on the value from another dataframe column 根据其他列值更改pandas DataFrame列值 - Change a pandas DataFrame column value based on another column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM