0

I have below python function to export data frame to csv file. I call it using below script

finalExport(dataAllR, sourcePath, header, started)
  • dataAllR: Name of dataframe
  • sourcePath: Path
  • header: List of columns
  • started: time

def finalExport(data, exportPath, header, te):
    print('# USDF: "finalExport" :-')
    exportPath = exportPath + '\\final_py_upload' + data + '.csv'
    data.to_csv(exportPath, columns = header, sep = ',', index = False)
    print('Process done, result file stored in: ', exportPath)
    if te != '': tpe(te)
    return

I want to use name of dataframe i.e dataAllR that I passed while calling function in script:

exportPath = exportPath + '\\final_py_upload' + data + '.csv'
#                                                ^

I want to generate file name as per data frame name.

Please help on this.

3
  • @Christian Dean, yes data.name Commented Aug 22, 2017 at 13:23
  • @Christian Dean, thanks a lot, I will check. Commented Aug 22, 2017 at 13:25
  • See my answer. I was a bit incorrect. You'll have to give your data frame a name yourself. Commented Aug 22, 2017 at 13:31

2 Answers 2

9

Since Python allows you to assign arbitrary attribute names to objects, you can assign an attribute named name to your dataframe to represent the name of it:

import pandas as pd 
df = pd.DataFrame()
df.name = 'My Data Frame'
print(df.name) # My Data Frame

In your case, after you define a name attribute for dataAllR:

dataAllR.name = 'dataAllR'

You would use:

exportPath = exportPath + '\\final_py_upload' + data.name + '.csv'

Or, even better:

exportPath = f'{exportPath}\\final_py_upload{data.name}.csv'
Sign up to request clarification or add additional context in comments.

3 Comments

Note that the f'...{}...' syntax is only available in Python 3.6 and up
@holdenweb Yes, I know that. He explicitly said in his title "python 3.6". I just edited out. See the edit history of the question. I'll edit in the Python 3.6 tag.
@Christian Dean, I used dataAllR.name = 'dataAllR', simple one working. Thanks
3

The fact of the matter is that Python values do not have names, they are bound to names (sometimes - see the second example). When I write

a = "a string"
b = a

what is the name of "a string"? a, or b, or both? Similarly, when I write

lst = ["orange", "banana", "apple"]

what is the name of "apple"? lst[2] isn't a name, it's a reference to an element of a container. This video by Ned Batchelder discusses potential sources of confusion to those still relatively new to Python.

Unfortunately (?) Pandas dataframes do not have a name attribute or anything like it. Given Python's flexibility it would be very easy to define a NamedDataFrame subclass that did maintain such an attribute, though you would then be faced with the question of how to derive those names in the first place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.