简体   繁体   中英

pandas increment row based on how many times a date is in a dataframe

i have this list for example dates = ["2020-2-1", "2020-2-3", "2020-5-8"] now i want to make a dataframe which contains only the month and year then the count of how many times it appeared, the output should be like

Date Count
2020-2 2
2020-5 1
import pandas as pd
dates = ["2020-2-1", "2020-2-3", "2020-5-8"]
df = pd.DataFrame({'Date':dates})
df['Date'] = df['Date'].str.slice(0,6)
df['Count'] = 1
df = df.groupby('Date').sum().reset_index()

Note: you might want to use the format "2020-02-01" with padded zeros so that the first 7 characters are always the year and month

This will give you a "month" and "year" column with the count of the year/month

If you want you could just combine the month/year columns together, but this will give you the results you expect if not exactly cleaned up.

df = pd.DataFrame({'Column1' : ["2020-2-1", "2020-2-3", "2020-5-8"]})
df['Month'] = pd.to_datetime(df['Column1']).dt.month
df['Year'] = pd.to_datetime(df['Column1']).dt.year
df.groupby(['Month', 'Year']).agg('count').reset_index()

Shorted code:

df['month_year'] = df['dates'].dt.to_period('M')

df1 = df.groupby('month_year')['dates'].count().reset_index(name="count")
print(df1)

month_year  count
0    2020-02      2
1    2020-05      1

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