python - How do you iterate through groups in a pandas Dataframe, operate on each group, then assign values to the original dataframe? -
yearcount = df[['antibiotic', 'order_date', 'antiyearcount']] yeargroups = yearcount.groupby('order_date') year in yeargroups: yearcount['antiyearcount'] =year.groupby('antibiotic'['antibiotic'].transform(pd.series.value_counts)
in case, yearcount dataframe containing 'order_date', 'antibiotic', 'antiyearcount'. have cleaned 'order_date' contain year of order. want group yearcount years in 'order_date', count number of times each 'antibiotic' appears in each "year group" assign value yearcount's 'antiyearcount' variable. help!
i think need add new column order_date
groupby
, possible usesize
instead pd.series.value_counts
same output:
df = pd.dataframe({'antibiotic':list('accbbb'), 'antiyearcount':[4,5,4,5,5,4], 'c':[7,8,9,4,2,3], 'd':[1,3,5,7,1,0], 'e':[5,3,6,9,2,4], 'order_date': pd.to_datetime(['2012-01-01']*3+['2012-01-02']*3)}) print (df) c d e antiyearcount antibiotic order_date 0 7 1 5 4 2012-01-01 1 8 3 3 5 c 2012-01-01 2 9 5 6 4 c 2012-01-01 3 4 7 9 5 b 2012-01-02 4 2 1 2 5 b 2012-01-02 5 3 0 4 4 b 2012-01-02 #copy remove warning #https://stackoverflow.com/a/45035966/2901002 yearcount = df[['antibiotic', 'order_date', 'antiyearcount']].copy() yearcount['antiyearcount'] = yearcount.groupby(['order_date','antibiotic'])['antibiotic'] \ .transform('size') print (yearcount) antibiotic order_date antiyearcount 0 2012-01-01 1 1 c 2012-01-01 2 2 c 2012-01-01 2 3 b 2012-01-02 3 4 b 2012-01-02 3 5 b 2012-01-02 3
yearcount['antiyearcount'] = yearcount.groupby(['order_date','antibiotic'])['antibiotic'] \ .transform(pd.series.value_counts) print (yearcount) antibiotic order_date antiyearcount 0 2012-01-01 1 1 c 2012-01-01 2 2 c 2012-01-01 2 3 b 2012-01-02 3 4 b 2012-01-02 3 5 b 2012-01-02 3
Comments
Post a Comment