Last active
August 29, 2015 14:00
-
-
Save olgabot/34c4b2d0f7302f3166f1 to your computer and use it in GitHub Desktop.
pandas groupby operate on specific columns and sub-columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json = '{"chrom":{"4010":"chr2","4011":"chr2","4012":"chr2","4013":"chr2","4014":"chr2","4015":"chr2","4016":"chr2","4017":"chr2","4018":"chr2","4019":"chr2","4020":"chr2","4021":"chr2","4022":"chr2","4023":"chr2","4024":"chr2","4025":"chr2"},"first_bp_intron":{"4010":50149390,"4011":50170930,"4012":50280729,"4013":50318633,"4014":50464109,"4015":50692700,"4016":50693626,"4017":50699610,"4018":50723234,"4019":50724853,"4020":50733756,"4021":50755790,"4022":50758569,"4023":50765775,"4024":51012497,"4025":51015345},"last_bp_intron":{"4010":50170841,"4011":50280408,"4012":50318460,"4013":50463926,"4014":50692579,"4015":50693598,"4016":50699435,"4017":50723042,"4018":50724470,"4019":50733632,"4020":50755762,"4021":50758364,"4022":50765390,"4023":50779724,"4024":51017681,"4025":51017681},"unique_junction_reads":{"4010":1,"4011":3,"4012":6,"4013":6,"4014":15,"4015":8,"4016":8,"4017":5,"4018":40,"4019":86,"4020":85,"4021":64,"4022":81,"4023":53,"4024":12,"4025":9}}' | |
sj = pd.read_json(json) | |
five_prime_reads = sj.groupby(('chrom', 'first_bp_intron')).apply(lambda x: x.unique_junction_reads.sum()) | |
three_prime_reads = sj.groupby(('chrom', 'last_bp_intron')).apply(lambda x: x.unique_junction_reads.sum()) | |
for (chrom, first_bp_intron , last_bp_intron), df in sj.groupby(['chrom', 'first_bp_intron', 'last_bp_intron']): | |
print chrom, last_bp_intron, | |
print '\tphi3', (df.unique_junction_reads/three_prime_reads[(chrom, last_bp_intron)]).values, | |
print '\tphi5', (df.unique_junction_reads/five_prime_reads[(chrom, first_bp_intron)]).values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment