"AttributeError" in a multiple dataframe for to_csv

  pandas, python

I am working with mutations and these are distribuited in 22 different files (working with only 22 chromosomes). While i am saving this files and adding a column (named as chrom) i finally get my multiple data frames:

import pandas as pd
import os
import glob
path=os.getcwd()
files=glob.glob(path+"/*snp.txt")
li=[]

for file in files:
    df=pd.read_csv(file,index_col=None,names=["pos","ref","AA"], delim_whitespace=True)
    df['chrom']=os.path.basename(file)
    df['chrom']=df.chrom.apply(lambda x: x.replace('snp.txt',''))
    li.append(df)
    li.to_csv(f'chrom{x.iloc[4,0]}_snp.csv')

But when i am trying to save this in different dataframes i got this warning:

Traceback (most recent call last):
File "<stdin>", line 6, in <module>
AttributeError: 'list' object has no attribute 'to_csv'*

I tried using dataframe=pd.DataFrame(li) but results as this:

 89 pos  ref AA chrom 0 100...
90 pos ref AA chrom 0 100...
91pos  ref AA chrom 0 100...  

How i convert my multiple dataframes to save em as csv?

Source: Python Questions

LEAVE A COMMENT