2017-01-18 112 views
1

我有2個DataFrames合併DataFrames

DF1由列:person_id,day,flag

DF2包括列:person_id,day_start,day_end

我想要添加到DF2列num_flags它會顯示flag列的總和在person_idday_start < day < day_end區間內。

哪種方法可以在沒有複雜循環的情況下做到這一點?我在尋找快速解決類似merge

回答

1
>>> df = pd.merge(df1,df2, on="person_id", how="outer") 
>>> df["lies_between"] = df.day.between(df.day_start, df.day_end,inclusive=False) 
>>> x = pd.pivot_table(df,values="flags",columns="lies_between", index="person_id",aggfunc=np.sum) 
>>> x.reset_index(drop=False,inplace=True) 
>>> x[["person_id", True]] 

這會有所幫助:

>>> help(pandas.Series.between_time)