【问题标题】:How to select rows from a dataframe based on conditions with another dataframe如何根据另一个数据框的条件从数据框中选择行
【发布时间】:2023-01-07 00:53:09
【问题描述】:

我有两个这样的数据框

df1:
Date       Fruit   
2022-11-24 Banana 
2021-5-23  Orange  
2020-10-1  Apple   
df2:
Start_Date End_Date    Fruit
2021-11-20 2022-12-31  Banana 
2022-6-1   2022-10-31  Banana   
2013-14-1  2022-11-31  Banana 
2013-11-24 2022-10-31  Celery 
2013-11-25 2022-10-31  Apple  
2013-11-25 2022-10-31  Orange  

我想创建一个数据框 df3,它由 df2 中的行组成,条件如下:

  • df1中有一行Fruit值相同,df1的值Date在df2的Start_Date和End_Date之间

所以对于这个例子,df3 将是:

df3:
Start_Date End_Date    Fruit
2021-11-20 2022-12-31  Banana    
2013-14-01 2022-11-31  Banana  
2013-11-25 2022-10-31  Apple  
2013-11-25 2022-10-31  Orange  

我试过了,但它似乎不是更优化的方式,并且需要时间来执行大型数据帧

df3 = pd.DataFrame()

for _, row in df1.iterrows():
    FRUIT= row['FRUIT']
    DATE= row['DATE']
    temp= df2[(df2['FRUIT'] == FRUIT) &(df2['Start_Date'] <= DATE) &(df2['End_Date'] >= DATE)] 
    
    df3 = pd.concat([df3 , temp])

谢谢您的帮助!

【问题讨论】:

    标签: python dataframe for-loop


    【解决方案1】:
    import pandas as pd
    
    # create a list to store the rows of df3
    df3_rows = []
    
    # iterate through the rows of df1
    for _, row in df1.iterrows():
      date = row['Date']
      fruit = row['Fruit']
      
      # use boolean indexing to select rows from df2 that meet the conditions
      df2_subset = df2[(df2['Fruit'] == fruit) & (df2['Start_Date'] <= date) & (df2['End_Date'] >= date)]
      
      # add the rows from df2_subset to df3_rows
      df3_rows.extend(df2_subset.to_dict('records'))
    
    # create df3 from the list of rows
    df3 = pd.DataFrame(df3_rows)
    

    【讨论】:

      【解决方案2】:
      #For 2013-14-1, there is no month 14, it would get error. 
      #And for 2022-11-31, there is no day 31 for Nov. 
      #Please correct these two records before running the following code.
      
      df1=pd.read_csv("test1.txt",parse_dates=[0],sep='	')
      df2=pd.read_csv("test2.txt",parse_dates=[0,1],sep='	') 
      #or you can also use pd.to_datetime to convert string into DateTime.
      print(df1.dtypes)
      print(df2.dtypes)
         
      df3=df2.assign(Date=df2.Fruit.map(df1.set_index('Fruit').Date.to_dict())).dropna(subset='Date')
      df3=df3.loc[df3.apply(lambda x:x.Date >= x.Start_Date and x.Date <= x.End_Date,axis=1)]
      print(df3)
      

      输出:

      Date     datetime64[ns]
      Fruit    object
      dtype: object
      
      Start_Date    datetime64[ns]
      End_Date      datetime64[ns]
      Fruit         object        
      dtype: object
      
      
        Start_Date   End_Date   Fruit       Date
      0 2021-11-20 2022-12-31  Banana 2022-11-24
      2 2013-12-01 2022-11-30  Banana 2022-11-24
      4 2013-11-25 2022-10-31  Apple  2020-10-01
      5 2013-11-25 2022-10-31  Orange 2021-05-23
      

      【讨论】:

        猜你喜欢
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 2017-03-20
        • 1970-01-01
        相关资源
        最近更新 更多