Pandas: Methods to populate a Column in a smaller DataFrame by checking multiple criteria in a Larger DataFrame?
I have two dataframes. The first dataframe is much smaller than the second (around 200 rows). I'm trying to add the T17, C19, C25, C30 and C21 columns from the second dataframe to the first dataframe ONLY when the cell meets the following criteria:
if merge_df\['Date Booked'\] \>= dom_rates_df\['begDate'\]
if merge_df\['Date Booked'\] \<= dom_rates_df\['endDate'\]
if (dom_rates_df\['rateSet'\] == merge_df\['Rate Zone'\]
[DataFrames](https://i.stack.imgur.com/1wc1k.png)
The difficult appears to lie in the fact that the tables are not the same size. The multiple conditions has been to difficult to implement using lambda functions or the .map() method.
I have tried this:
\[x for x in dom_rates_df\['T17'\] if merge_df\['Date Booked'\] \>= dom_rates_df\['begDate'\]\]# if merge_df\[开发者_如何学Go'Date Booked'\] \<= dom_rates_df\['endDate'\] if (dom_rates_df\['rateSet'\] == merge_df\['Rate Zone'\])\]
And this:
def calculate_category(dom_rates_df, merge_df):
category_row = dom_rates_df\[(dom_rates_df\["rateSet"\] == merge_df\["Rate Zone"\]) & ( dom_rates_df\["begDate"\] \<= merge_df\["Date Booked"\] & (dom_rates_df\['endDate'\] \>= merge_df\['Date Booked'\]))\]
return (category_row\['T17'\])
df.apply(lambda merge_df: calculate_category(dom_rates_df, merge_df), axis=1)
精彩评论