0

I have a folder with around 1000 .txt files, and I need to run the same code on each of them. One particular thing I need to keep track of is the count of a particular haplotype. I used hap = df['hap'].value_counts().to_frame() to create a new data frame with the counts of each haplotype. It looks something like this.

hap        count
1      347
5      171
3      168
7      140
6      56
11     51
9      33
2      31
10     3

I was wondering if there was a way for me to extract the count of just haplotype 7 and store its value in a variable.

One method that I have used is to use the df['haplotype'].tolist() command and then run a for loop with a basic if-else clause that keeps track of the count haplotype 7, according to the number of times it occurs in the list. But I'm curious to know if I can access it in the manner I've described above.

vezunchik
  • 3,669
  • 3
  • 16
  • 25

1 Answers1

0

You can get the row where hap is 7 using the [] operator.

cnt = df[df['hap']==7]['count']
Vishnu Dasu
  • 533
  • 5
  • 18