pandas - filling columns based on multiple row values in Python -
i have dataframe a
empty cells want fill have dataframe b
. here sample data:
a= month type sale 2016-01 20 2016-02 10 2016-01 b 40 2016-02 b 30 2016-01 c 2016-02 c 2016-01 d 2016-02 d b= month type sale 2016-01 20 2016-02 10 2016-01 b 40 2016-02 b 30 2016-01 c 60 2016-02 c 40 2016-01 d 60 2016-02 d 40
here have done:
empttypes= ['c', 'd'] x = a.groupby('month', sort = false).sale.sum() b['sale'][b['type'].isin(empttypes) & b['month'].isin(x.index)]=x
and nothing happens!
df['sale'] = df.groupby('month', sort = false).sale.apply(lambda x: x.fillna(x.sum())) print (df) month type sale 0 2016-01 20.0 1 2016-02 10.0 2 2016-01 b 40.0 3 2016-02 b 30.0 4 2016-01 c 60.0 5 2016-02 c 40.0 6 2016-01 d 60.0 7 2016-02 d 40.0