8

I have problems converting the variable into a string. It doesn't work with str(object), this prints out the dataframe instead. I labelled my dataframe as Apple:

Apple = pd.Dataframe()

Then I was trying to do a for loop to save the the data into another dataframe. So i did it by: First, I stored the stocks in apple_stock_list, [Apple, Samsung] Then I wanted to do a for loop like this:

for stocks in apple_stock_list:
    for feature in features:
        apple_stock[str(stocks) + "_"+ feature] = stocks[feature]

However, str(stocks) does not change stocks into "Apple". Is there anyway I could get the Dataframe's variable name to be a string?

Thank you in advance!

0

1 Answer 1

5

Considering the following df

import pandas as pd
import numpy as np

apple = pd.DataFrame(data=np.ones([5,5]))

As @Georgy suggests one can name the dataframe like this

apple.name = 'Apple'

Then get the string with apple.name.

[In]: print(apple.name) 
[Out]: Apple

However, assuming one wants to get the DFs from a particular label, one might as well create a dictionary with

dfdict = {'Apple': apple}

and access the dataframe via

[In]: print(dfdict['Apple'])
[Out]:
     0    1    2    3    4
0  1.0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0  1.0
Sign up to request clarification or add additional context in comments.

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.