如何根据具有日期范围的另一个 DataFrame 中的列在 DataFrame 中搜索价格列

我有一个名为 data 的 DataFrame,其中包含每台笔记本电脑的日期和价格。


              Acer      Mac   Toshiba


date                                                                          

2019-01-04  754.23  1173.08    969.15  

2019-01-05  753.69  1176.14    969.42  

2019-01-11  754.21  1171.56    970.30  

2019-01-28  752.61  1153.29    964.96  

2019-01-29  753.43  1148.72    964.43  


2019-02-03  754.27  1157.23    966.60  

2019-02-04  752.97  1150.68    964.72  

2019-02-09  753.25  1152.49    965.20  

2019-02-19  754.25  1154.49    963.20  

2019-02-26  752.25  1155.49    962.20


2019-03-01  753.48  1153.13    966.18  

2019-03-30  753.49  1156.94    966.96         

我有另一个名为笔记本电脑的 DataFrame,其中包含开始和结束日期


                 start         end     Laptop


2019-01-29  2010-01-04  2010-01-29       Acer

2019-02-26  2010-02-03  2010-02-26    Toshiba

2019-03-30  2019-03-01  2019-03-30        Mac

如何在日期范围内打印第二个 DataFrame 中所述的笔记本电脑的价格,使其看起来像这样:


              Laptop    Price   


date                                                                          

2019-01-04      Acer    754.23  

2019-01-05      Acer    753.69  

2019-01-11      Acer    754.21  

2019-01-28      Acer    752.61  

2019-01-29      Acer    753.43  


2019-02-03   Toshiba    966.60  

2019-02-04   Toshiba    964.72  

2019-02-09   Toshiba    965.20  

2019-02-19   Toshiba    963.20  

2019-02-26   Toshiba    962.20


2019-03-01       Mac   1153.13    

2019-03-30       Mac   1156.94    

以下是我所拥有的,但它不打印价格:


for date in data.index:

    for date1 in laptop['end']:

        if date == date1:

            start = laptop['start']

            end = laptop['end']

            laptop = laptop['Laptop']

    p = data.loc[start: end, laptop]    #to search for the prices for the laptop chosen in the respective date range 


print(p)  

我收到以下错误:


ValueError:基于位置的索引只能有[标签(必须在索引中),标签切片(包括两个端点!如果索引是整数,可以是整数切片),标签列表,布尔]类型


任何帮助,将不胜感激。


慕侠2389804
浏览 97回答 1
1回答

扬帆大鱼

我认为日期laptop很糟糕,因为有些年份是 2010 年。我已将其更改为 2019 以获得所需的结果笔记本电脑是:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;start&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end&nbsp; &nbsp; &nbsp;Laptop2019-01-29&nbsp; 2019-01-04&nbsp; 2019-01-29&nbsp; &nbsp; &nbsp; &nbsp;Acer2019-02-26&nbsp; 2019-02-03&nbsp; 2019-02-26&nbsp; &nbsp; Toshiba2019-03-30&nbsp; 2019-03-01&nbsp; 2019-03-30&nbsp; &nbsp; &nbsp; &nbsp; Mac您可以使用:lst=[]i=0while i<len(laptop.index):&nbsp; &nbsp; data['Laptop']=laptop.loc[laptop.index[i],'Laptop']&nbsp; &nbsp; lst.append((data.loc[(laptop.loc[laptop.index[i],'start']<=data.index) & (laptop.loc[laptop.index[i],'end']>=data.index)][['Laptop',data['Laptop'][0]]].rename(columns={data['Laptop'][0]:'Prices'})))&nbsp; &nbsp; print(lst[i])&nbsp; &nbsp; print('-'*50)&nbsp; &nbsp; i+=1new_df=pd.concat([df for df in lst])data=data.drop(['Laptop'], axis=1)print(new_df)输出:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Laptop&nbsp; Pricesdate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2019-01-04&nbsp; &nbsp;Acer&nbsp; &nbsp; 754.232019-01-05&nbsp; &nbsp;Acer&nbsp; &nbsp; 753.692019-01-11&nbsp; &nbsp;Acer&nbsp; &nbsp; 754.212019-01-28&nbsp; &nbsp;Acer&nbsp; &nbsp; 752.612019-01-29&nbsp; &nbsp;Acer&nbsp; &nbsp; 753.43--------------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Laptop&nbsp; Pricesdate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2019-02-03&nbsp; Toshiba&nbsp; 966.602019-02-04&nbsp; Toshiba&nbsp; 964.722019-02-09&nbsp; Toshiba&nbsp; 965.202019-02-19&nbsp; Toshiba&nbsp; 963.202019-02-26&nbsp; Toshiba&nbsp; 962.20--------------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Laptop&nbsp; Pricesdate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2019-03-01&nbsp; &nbsp; Mac&nbsp; &nbsp; 1153.132019-03-30&nbsp; &nbsp; Mac&nbsp; &nbsp; 1156.94--------------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Laptop&nbsp; &nbsp;Pricesdate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2019-01-04&nbsp; &nbsp; &nbsp;Acer&nbsp; &nbsp;754.232019-01-05&nbsp; &nbsp; &nbsp;Acer&nbsp; &nbsp;753.692019-01-11&nbsp; &nbsp; &nbsp;Acer&nbsp; &nbsp;754.212019-01-28&nbsp; &nbsp; &nbsp;Acer&nbsp; &nbsp;752.612019-01-29&nbsp; &nbsp; &nbsp;Acer&nbsp; &nbsp;753.432019-02-03&nbsp; Toshiba&nbsp; &nbsp;966.602019-02-04&nbsp; Toshiba&nbsp; &nbsp;964.722019-02-09&nbsp; Toshiba&nbsp; &nbsp;965.202019-02-19&nbsp; Toshiba&nbsp; &nbsp;963.202019-02-26&nbsp; Toshiba&nbsp; &nbsp;962.202019-03-01&nbsp; &nbsp; &nbsp; Mac&nbsp; 1153.132019-03-30&nbsp; &nbsp; &nbsp; Mac&nbsp; 1156.94我已经连接了DataFrame每个laptop你可以离开而不连接的
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python