Skip to content
Advertisement

write filename when iterating through dataframes

I am passing to a function several pandas df:

def write_df_to_disk(*args):
    for df in args:
        df.to_csv('/transformed/'+str(df)+'.table',sep='t')


write_df_to_disk(k562,hepg2,hoel)

df here will be a pandas dataframe.

How can I assign the diferent parameters of *args to a string like above '/transformed/'+str(df)+'.table',sep='t' ??

I want to have three files written to disk with the following path:

`/transformed/k562.table`
`/transformed/hepg2.table`
`transformed/hoel.table`

Advertisement

Answer

ok I think I managed.

def write_df_to_disk(l,*args):
    for l,df in zip(l,args):
        df.to_csv('transformed/'+l+'.table',sep='t')

write_df_to_disk(['k562','hepg2','hoel'],k562,hepg2,hoel)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement