python assign value to pandas df if falls between range of dates in another df

前端 未结 2 524
面向向阳花
面向向阳花 2021-01-07 07:44

What is the best way to create a new column and assign a value if date falls between two dates in another dataframe ?

e.g.

dataframe A    
date             


        
2条回答
  •  一整个雨季
    2021-01-07 07:56

    Using an IntervalIndex, which is new in Pandas 0.20.0. This looks to still be in the experimental phase though, so other solutions may be more reliable.

    # Get the 'id' column indexed by the 'start'/'end' intervals.
    s = pd.Series(df_b['id'].values, pd.IntervalIndex.from_arrays(df_b['start'], df_b['end']))
    
    # Map based on the date of df_a.
    df_a['id'] = df_a['date'].map(s)
    

    The resulting output:

            date values  id
    0 2017-05-16      x  34
    1 2017-04-12      Y  32
    

    Alternatively, if you don't mind altering the index of df_b, you could just directly convert to an IntervalIndex on it:

    # Create an IntervalIndex on df_b.
    df_b = df_b.set_index(['start', 'end'])
    df_b.index = pd.IntervalIndex.from_tuples(df_b.index)
    
    # Map based on the date of df_a.
    df_a['id'] = df_a['date'].map(df_b['id'])
    

提交回复
热议问题