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

前端 未结 2 548
面向向阳花
面向向阳花 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 08:17

    This is an example of something that's really straightforward to do in one step in sql, but not so much in Pandas. So with the proviso that I don't love this approach, here it is.

    1. do a full cartesian join
    2. filter down to desired rows & columns.

    _

    # First Full Outer Join Dataframes 
    # (Requires a Common Column in Pandas Unlike SQL)
    df_A['fake key'] = 1
    df_B['fake key'] = 1
    outer_join = pd.merge(df_A, df_B, how='outer', on='fake key')
    
    # Now Filter Back down to Desired Rows/Columns
    desired_rows    = outer_join.query('date < end and date > start')
    desired_columns = ['date', 'values', 'id']
    
    final = desired_rows[desired_columns]
    final
    

    output:

            date values  id
    0 2017-05-16      x  34
    5 2017-04-12      y  32
    

    The things that make this answer somewhat unsatisfying to me are:

    1. The first step of a full cartesian join doesn't scale well at all to large data
    2. The cartesian join requires a common column, so in this case first creating that fake key column (see this github issue

提交回复
热议问题