Select DataFrame rows between two dates

前端 未结 10 877
挽巷
挽巷 2020-11-22 03:14

I am creating a DataFrame from a csv as follows:

stock = pd.read_csv(\'data_in/\' + filename + \'.csv\', skipinitialspace=True)

The DataFra

10条回答
  •  梦如初夏
    2020-11-22 03:59

    Keeping the solution simple and pythonic, I would suggest you to try this.

    In case if you are going to do this frequently the best solution would be to first set the date column as index which will convert the column in DateTimeIndex and use the following condition to slice any range of dates.

    import pandas as pd
    
    data_frame = data_frame.set_index('date')
    
    df = data_frame[(data_frame.index > '2017-08-10') & (data_frame.index <= '2017-08-15')]
    

提交回复
热议问题