How to save a dataframe with the same dataframe name in python? In order to do that I need to extract the name of the dataframe and use it as a text to save to csv file. like dataframe.to_csv('name of the dataframe'+'.csv). My question is how to extract the name of the dataframe. Example: I have a dataframe that is randomly generated as XX_20135. XX_20135.to_csv('XX_20135.csv') so in the output I will have the csv name as XX_20135. I don't know the name of the df in advance as it is generated randomly.
Asked
Active
Viewed 190 times
0
-
have a look here. It is not safe to work and not recommended, but that's what you are searching for right? https://stackoverflow.com/a/50620134/15521392 – Rabinzel Aug 22 '22 at 21:24
-
Yes, sounds like in the very last reply would to the job. Thank you! def aux_retrieve_name(var): callers_local_vars = inspect.currentframe().f_back.f_back.f_locals.items() return [var_name for var_name, var_val in callers_local_vars if var_val is var] def header_generator(df): print('--------- Feature Analyzer ----------') print('Dataframe name: "{}"'.format(aux_retrieve_name(df))) print('Memory usage: {:03.2f} MB'.format(df.memory_usage(deep=True).sum() / 1024 ** 2)) return – anonymous Aug 22 '22 at 21:35
1 Answers
0
Isn't the dataframe name the same as variable name? If so, the variable name is fixed, isn't it? If it is fixed, you know it when running the code.

Girts Strazdins
- 848
- 9
- 15
-
Yes, when the dataframe is randomly generated, I would know the name but still I need a command to extract the name of the dataframe as a string to be able to save the file with the same name. The question is how to extract the dataframe name as a string. Sound simple but did not find a way in my search. – anonymous Aug 22 '22 at 21:23