Entering edit mode
4.7 years ago
Ashley
▴
90
Hi all
I have a question about data operation.
I have a file:
a 100
a 20
b 40
c 30
a 60
a 70
I want to obtain the result:
a 60(the average of 100 and 20)
b 40
c 30
d 65the average of 60 and 70)
the process can be described as computing the average value of col2 based on index col1.
I have shell code, but I don't know how to convert to python code.
cat 1.txt |awk -F"," '{sum[$1]+=$2}END{for(c in sum){print c,sum[c]}}'
Any help would be greatly appreciated.