Reputation: 864
I have five DataFrames, df1
, df2
, df3
, df4
, df5
.
I can save each one individually with:
df1.to_csv('~/Desktop/df1.tsv', index=False, header=False, sep='\t')
df2.to_csv('~/Desktop/df2.tsv', index=False, header=False, sep='\t')
...
Can I do this in a loop where the file path ends with the name of the variable that holds the Dataframe?
Upvotes: 1
Views: 570
Reputation: 2409
when you say
the name of the DataFrame
do you mean the name of the variable that holds the dataframe? that won't work.
but here's something that will work:
dfs = [df1, df2, df3, df4, df5]
for i, df in enumerate(dfs):
df.to_csv(f'~/Desktop/df{i}.tsv', index=False, header=False, sep='\t')
if you wanted unique names
dfs = [df1, df2, df3, df4, df5]
names = ["apple", "bananna", "cherry", "drizz", "ebola"]
for name, df in zip(names, dfs):
df.to_csv(f'~/Desktop/df{name}.tsv', index=False, header=False, sep='\t')
Upvotes: 4