I created a pandas mn data file using the following input:
keyA state n1 n2 d1 d2 key1 CA 100 1000 1 2 key2 FL 200 2000 2 4 key1 CA 300 3000 3 6 key1 AL 400 4000 4 8 key2 FL 500 5000 5 2 key1 NY 600 6000 6 4 key2 CA 700 7000 7 6
Created an amount object as follows:
s = mn.groupby(['keyA','state'], as_index=False).sum()
How to iterate over a sum s object, so I can get the following output:
Column v1 in the result below is calculated as s['n1']/s['d1']
Column v2 in the result below is calculated as s['n2']/s['d2']
keyA state v1 v2 'key1','AL',100,500 'key1','CA',100,500 'key1','NY',100,1500 'key2','CA',100,1166 'key2','FL',100,1166
python pandas
user3376169
source share