首页 > 解决方案 > 我如何从 pandas.core.frame.DataFrame 中选择特定变量?

问题描述

我希望在这些 DataFrames 中选择一个特定的日期。我只想选择日期2021-09-03(并查看 adj 关闭和信号)我该怎么做?而且我不想看到没有我想要的日期的 DataFrame。

我试图在这段代码中看到这一点,而为我们提供代码的 DataFrame 是代码df.buyingSignals()下方的 2 个表:

acciones=['FCEL','CAT']
for accion in acciones:
    accion=Ticker(accion, "2021-1-1", "2021-9-20", [5, 10, 20])
    accion.buyingSignals()
    #np.where(accion.loc['2021-9-03'],accion.buyingSignals(),0) #Possible answer that i think but it's not working
[*********************100%***********************]  1 of 1 completed


 ********** FCEL **********
            Adj Close  Signal
Date                         
2021-02-02  21.430000       1
2021-02-09  27.959999       1
2021-06-02  10.430000       1
2021-08-31   6.240000       1
[*********************100%***********************]  1 of 1 completed


 ********** CAT **********
             Adj Close  Signal
Date                          
2021-02-19  207.849991       1
2021-03-11  217.603302       1
2021-04-01  230.455948       1
2021-04-20  225.396088       1
2021-05-04  232.164093       1
2021-05-10  241.314987       1
2021-05-17  243.483337       1
2021-05-19  235.605621       1
2021-06-09  233.397476       1
2021-07-15  210.281525       1
2021-08-13  218.570007       1
2021-08-31  210.869995       1
2021-09-03  210.369995       1

标签: pythonpandasdataframe

解决方案


只需使用loc

>>> df.loc['2021-09-03']
Adj Close    210.369995
Signal         1.000000
Name: 2021-09-03, dtype: float64
>>> 

推荐阅读