简体   繁体   中英

How can i set a date interval for my data in python?

Im desperatly trying group my data inorder to see which months most people travel but first i want to remove all the data from before a certain year.

As you can see in the picture, i've data all the way back to the year 0003 which i do not want to include.

How can i set an interval from 1938-01-01 to 2020-09-21 with pandas and datetime

My_Code

One way to solve this is:

Verify that the date is on datetime format (its neccesary to convert this)

df.date_start = pd.to_datetime(df.date_start)

Set date_start as new index:

df.index = df.date_start

Apply this

df.groupby([pd.Grouper(freq = "1M"), "country_code"]) \
      .agg({"Name of the column with frequencies": np.sum})

Boolean indexing with pandas.DataFrame.Series.between

# sample data
df = pd.DataFrame(pd.date_range('1910-01-01', '2020-09-21', periods=10), columns=['Date'])

# boolean indexing with DataFrame.Series.between
new_df = df[df['Date'].between('1938-01-01', '2020-09-21')]
# groupby month and get the count of each group
months = new_df.groupby(new_df['Date'].dt.month).count()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM