How do I filter data by date in a dataframe (Python)

2

Being that you would have to create a new dataframe with the information coming from those dates below.

import pandas as pd
import numpy as np
import datetime
%matplotlib inline
races = pd.read_csv('races.csv')
results = pd.read_csv('results.csv')
last10M = pd.merge(results, races, how='outer', on='raceId')
start = datetime.datetime(int(2008), int(4), 20)
end = datetime.datetime(int(2008), int(4), 20)

I'm stuck in this last part, the dates I've already done, I'm just missing the filtering of the 'last10M' dataframe. The 'last10M' dataframe would have to be filtered in the 'date' field from 2008 to 2018

    
asked by anonymous 20.04.2018 / 10:11

1 answer

3

Try the following:

races = pd.read_csv('races.csv', parse_dates=['date']) # tratar a coluna date como datetime
results = pd.read_csv('results.csv')
last10M = pd.merge(results, races, how='outer', on='raceId')
interval = (last10M['date'] > '2008-01-01') & (last10M['date'] <= '2018-01-01')
df_interval = last10M.loc[interval]
    
20.04.2018 / 11:25