Home > other >  How do I save a dataframe in the name of a variable I created earlier in the code (oldest_id and iso
How do I save a dataframe in the name of a variable I created earlier in the code (oldest_id and iso

Time:09-02

#fetch the data in a sequence of 1 million rows as dataframe
df1 = My_functions.get_ais_data(json1)
df2 = My_functions.get_ais_data(json2)
df3 = My_functions.get_ais_data(json3)
df_all = pd.concat([df1,df2,df3], axis = 0 )

#save the data frame with names of the oldest_id and the corresponding iso data format
df_all.to_csv('oldest_id   iso_date  .csv')

.....the last line might be silly but I am trying to save the data frame in the name of some variables I created earlier in the code.

CodePudding user response:

You can use an f-string to embed variables in strings like this:

df_all.to_csv(f'/path/to/folder/{oldest_id}{iso_date}.csv')

CodePudding user response:

Maybe try:

file_name = f"{oldest_id}{iso_date}.csv"
df_all.to_csv(file_name)

Assuming you are using Python 3.6 and up.

CodePudding user response:

if you need the value corresponding to the variable then mids answer is correct thus:

df_all.to_csv(f'/path/to/folder/{oldest_id}{iso_date}.csv')

However if you want to use the name of the variable itselfs :

df_all.to_csv('/path/to/folder/' f'{oldest_id=}'.split('=')[0] f'{iso_date=}'.split('=')[0] '.csv') would do the work

  • Related