# exporting table to filter out counts with python

**URL:** https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677
**Category:** Other Bioinformatics Tools
**Created:** [April 4, 2022, 7:00pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677 "2022-04-04T19:00:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rnasrah](https://forum.qiime2.org/user_avatar/forum.qiime2.org/rnasrah/32/13101_2.png) [@rnasrah](https://forum.qiime2.org/u/rnasrah)
#### Post date: [April 4, 2022, 7:00pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/1 "2022-04-04T19:00:13Z")

</div>

Hello,  
I would like to do the following:  
filter out features that are in counts of 10 or less IN a sample (rather than across samples). I have posted about this [previously](https://forum.qiime2.org/t/filtering-features-frequency-per-sample-instead-of-across-samples/16732) and one recommendation was to export my feature table and filter in python. I am wondering how best to do this.

My python is basic so I was wondering if I can get help with this. Would my script below make sense?

qiime tools export   
--input-path table.qza   
--output-path exported-table  
biom convert -i exported-table/feature-table.biom -o exported-table/feature-table.tsv --to-tsv

import pandas as pd  
df = pd.read\_csv("exported-table/feature-table.tsv",sep='\t')

df[df \<11] = 0

Thank you so much!

---

<div class="post-metadata">

### Author: ![timanix](https://forum.qiime2.org/user_avatar/forum.qiime2.org/timanix/32/17879_2.png) [@timanix](https://forum.qiime2.org/u/timanix)
#### Post date: [April 4, 2022, 7:59pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/2 "2022-04-04T19:59:28Z")

</div>

Hello!  
That's making sense to me.  
You should try it.  
If you will get errors while reading table, try slightly modified code:

```auto
import pandas as pd
df = pd.read_csv("exported-table/feature-table.tsv",sep='\t', skiprows=1,index_col=0) #skip first row (#constructed from biom), set feature IDs to be an index to avoid format issues. 

df[df <11] = 0
df.to_csv('filtered_table.tsv',sep='\t')

```

---

<div class="post-metadata">

### Author: ![rnasrah](https://forum.qiime2.org/user_avatar/forum.qiime2.org/rnasrah/32/13101_2.png) [@rnasrah](https://forum.qiime2.org/u/rnasrah)
#### Post date: [April 4, 2022, 11:26pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/3 "2022-04-04T23:26:41Z")

</div>

> [@timanix](#):
>
> ```auto
> import pandas as pd
> df = pd.read_csv("exported-table/feature-table.tsv",sep='\t', skiprows=1,index_col=0) #skip first row (#constructed from biom), set feature IDs to be an index to avoid format issues. 
> 
> df[df <11] = 0
> df.to_csv('filtered_table.tsv',sep='\t')
> 
> ```

Thanks so much @timanix! Worked perfectly.  
How can I convert the filtered\_table.tsv back to a feature table. I'm guessing I can do it in two steps  
1- first to convert it back to a biom file (not sure how to do this)  
then  
2- convert the biom file to a feature table with below script:

qiime tools import   
--input-path feature-table.biom   
--type 'FeatureTable[Frequency]'   
--input-format BIOMV100Format   
--output-path feature-table-1.qza

Thanks again!

---

<div class="post-metadata">

### Author: ![timanix](https://forum.qiime2.org/user_avatar/forum.qiime2.org/timanix/32/17879_2.png) [@timanix](https://forum.qiime2.org/u/timanix)
#### Post date: [April 5, 2022, 3:51am UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/4 "2022-04-05T03:51:07Z")

</div>

For this, follow the instructions as described [here](https://forum.qiime2.org/t/converting-and-exporting-tsv-to-biom/3934/2).

---

<div class="post-metadata">

### Author: ![rnasrah](https://forum.qiime2.org/user_avatar/forum.qiime2.org/rnasrah/32/13101_2.png) [@rnasrah](https://forum.qiime2.org/u/rnasrah)
#### Post date: [April 5, 2022, 4:42pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/5 "2022-04-05T16:42:09Z")

</div>

Thank you!  
Although I'm not sure what to use for the 'input format' for the qiime tools import command - BIOMV100Format or BIOMV210Format? I tried reading the Biom file format [docs](http://biom-format.org/documentation/format_versions/biom-2.1.html) but still not sure.  
Any ideas which would I should choose?

Thanks

---

<div class="post-metadata">

### Author: ![timanix](https://forum.qiime2.org/user_avatar/forum.qiime2.org/timanix/32/17879_2.png) [@timanix](https://forum.qiime2.org/u/timanix)
#### Post date: [April 5, 2022, 4:53pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/6 "2022-04-05T16:53:07Z")

</div>

Usually BIOMV210Format works for me without any issues

---

<div class="post-metadata">

### Author: ![Nick\_Gabry](https://forum.qiime2.org/user_avatar/forum.qiime2.org/nick_gabry/32/7867_2.png) [@Nick\_Gabry](https://forum.qiime2.org/u/Nick_Gabry)
#### Post date: [April 14, 2022, 11:38am UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/7 "2022-04-14T11:38:30Z")

</div>

If you're still looking for help with this, I've made command line / python toolkit that has this functionality which you can easily install with conda in your Qiime environment!

> **[GitHub - NGabry/MetaPlex: Read processing toolkit for the MetaPlex NGS...](https://github.com/NGabry/MetaPlex)**
>
> Read processing toolkit for the MetaPlex NGS metabarcoding workflow

Check out the Per Sample Filtering section, where you can set a single integer level to filter at within each sample, our input a .csv with unique filtering levels for each sample.

---

<div class="post-metadata">

### Author: ![rnasrah](https://forum.qiime2.org/user_avatar/forum.qiime2.org/rnasrah/32/13101_2.png) [@rnasrah](https://forum.qiime2.org/u/rnasrah)
#### Post date: [April 20, 2022, 6:55pm UTC](https://forum.qiime2.org/t/exporting-table-to-filter-out-counts-with-python/22677/8 "2022-04-20T18:55:46Z")

</div>

Thanks so much @Nick_Gabry !
