2016-11-10 9 views
0

私は1年間の価値のあるスポーツイベントのフィクスチャを含むpandas Dataframeを持っています。週ごとのパンダデータフレームグループ

データフレームに新しい列を作成したいのですが、ラウンドごとに大きな行列のセットを扱うことができるように、週ごとにインクリメントする 'round'です。

あなただけの日付でソートされている各週の異なる整数値を持つ新しい列を、必要があるが、あなたは彼らが取るんどの値を気にしない、あなたが試みることができると仮定すると、
import pandas as pd 
dta = pd.read_csv(...) #read data 
indexedDta = dta.set_index(['Date']) #index the raw data. 
indexedDta['Round'] = 0 #add in the new column and give dummy value. 
indexedDta['Round']= indexedDta.groupby(by=dta.Date) #wrong 
+0

この列は、新しい週ごとに1ずつインクリメントする必要がありますか(つまり、 '... 4,5,6 ... ')か、 8、11 ... ')? – Peque

+0

1を1ずつインクリメント – Mdev

答えて

0

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 

enter image description here

あなたは多分追加し、それが1ずつインクリメントされたい場合は、次の

df['round'] = df['round'].diff(1).fillna(1.) 
df.loc[df['round'] != 0, 'round'] = 1. 
df['round'] = df['round'].cumsum().astype(int) 

df 

enter image description here

関連する問題