0

I want to print the name of the Dataframe in a for a loop, but I dont get it right When I iterate over the datasets list I get the dataset. If I try with str(d) for example I get all the dataset as string. d.name() doesnt work either.

What can I do to print just the name of the Dataframe as a string?

Thanks in advance!

PD: I get this Error, "AttributeError: 'DataFrame' object has no attribute 'name'"

# Define lists
datasets = [train_data, test_data]
features = ['Age', 'Fare']
# Create function
fig, outliers = plt.subplots(figsize=(20,10), ncols=4)
row, col = 0, 0
for f in features:
    for d in datasets:
        sns.boxplot(x=d[f], orient='v', color=pal_titanic[3], ax=outliers[col])
        outliers[col].set_title(f + 'in' + d)
        col = col + 1    
l.legren
  • 161
  • 1
  • 2
  • 13
  • Just to be clear, what do you mean exactly by the dataframe names? – strivn May 13 '20 at 16:33
  • Well, its not exactly the name, but the dataset name as it appears in the list. In this case, it would be the name of the variables, train_data and test_data I'm iterating over. As far as I read the __name__ would refer to something else. – l.legren May 13 '20 at 16:37
  • 1
    I'm unsure, but afaik the variable names are mapping to objects, and you can't really get python variable names in a straight forward manner. You may want to read [this](https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string/18425523). Another method is perhaps create a dictionary like so: `datasets={'train_data': train_data, 'test_data': test_data} and you can use the keys as your dataset names. – strivn May 13 '20 at 16:50
  • You could also just add a `name` attribute to your DataFrame instances and use it later. See [this answer](https://stackoverflow.com/questions/14688306/adding-meta-information-metadata-to-pandas-dataframe) for details. – amain May 13 '20 at 18:37

0 Answers0