Convert pd.DataFrame to Metadata?

Hi,

I am using qiime2’s python API (version 2019.1) now and it’s pretty good. I am wondering if there is any way to convert pd.DataFrame back to metadata? Or I have to export the dataframe as .tsv file and reload the metadata again?

Thanks.

Jiung-Wen

Hi @jwchen,

I just pass my metadata into the Metadata object:

from qiime2 import Metadata
meta_df = pd.read_csv('my_metadata.tsv', sep='\t').set_index('sample_id')
meta_q2 = Metdata(meta)

There are a couple of caveats. If you’ve been using the nifty pandas Categorical types to handle your data for ordinal stuff, QIIME will not be happy. I’m also not sure if it automagically casts your numbers, either, so I’d watch that.

Best,
Justine

4 Likes

Thank you Justine!! It works!!