pythonpandasdataframesaveexploratory-data-analysis

How do I save a dataframe in the name of a variable I created earlier in the code (oldest_id and iso_data as seen in the code)


#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.


Solution

  • 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