Populate column in data frame based on a range found in another dataframe

前端 未结 3 1530
生来不讨喜
生来不讨喜 2021-01-25 18:07

I\'m attempting to populate a column in a data frame based on whether the index value of that record falls within a range defined by two columns in another data frame.

d

3条回答
  •  长发绾君心
    2021-01-25 18:39

    You can use IntervalIndex (requires v0.20.0).

    First construct the index:

    df2.index = pd.IntervalIndex.from_arrays(df2['START'], df2['STOP'], closed='both')
    
    df2
    Out: 
            START  STOP  CLASS
    [2, 3]      2     3      1
    [5, 7]      5     7      2
    [8, 8]      8     8      3
    

    Now if you index into the second DataFrame it will lookup the value in the intervals. For example,

    df2.loc[6]
    Out: 
    START    5
    STOP     7
    CLASS    2
    Name: [5, 7], dtype: int64
    

    returns the second class. I don't know if it can be used with merge or with merge_asof but as an alternative you can use map:

    df1['CLASS'] = df1.index.to_series().map(df2['CLASS'])
    

    Note that I first converted the index to a Series to be able to use the Series.map method. This results in

    df1
    Out: 
         a  CLASS
    0    4    NaN
    1   45    NaN
    2    7    1.0
    3    5    1.0
    4   48    NaN
    5   44    2.0
    6   22    2.0
    7   89    2.0
    8   45    3.0
    9   44    NaN
    10  23    NaN
    

提交回复
热议问题