Pandas Dataframe group by week -
i have pandas dataframe contains set of fixtures of sports events on years worth of data.
i want create new column in dataframe, 'round' increments week week can work on larger set of matrices (of fixtures) round round.
import pandas pd dta = pd.read_csv(...) #read data indexeddta = dta.set_index(['date']) #index raw data. indexeddta['round'] = 0 #add in new column , give dummy value. indexeddta['round']= indexeddta.groupby(by=dta.date) #wrong
assuming need new column different integer values each week, sorted date, not care values take, can try:
import pandas data = list(range(10)) dates = pandas.series(pandas.date_range('2013-11-01', '2013-11-10')) df = pandas.dataframe({'dates': dates, 'data': data}) df = df.set_index('dates') df['round'] = df.index.year * 100 + df.index.week df
if incremented 1 1, maybe add:
df['round'] = df['round'].diff(1).fillna(1.) df.loc[df['round'] != 0, 'round'] = 1. df['round'] = df['round'].cumsum().astype(int) df
Comments
Post a Comment