Python Forum

Full Version: Creating A List of DataFrames & Manipulating Columns in Each DataFrame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone!
Would you help solving the following problem. I want to create a list made up of several dataframes, access each dataframe through a loop and once a dataframe has been reached, use another loop to access the column(s) of that dataframe and print elements of the column(s) or do something else with it.

The output i want to get through that process should look something like this:
208.185 140.87 237.11
208.76 140.14 232.9
209.59 140.91 237.47
207.575 139.8005 232.18

But this is what i currently get using the current script:

[0 207.805
1 208.760
Name: AAPL, dtype: float64, 0 140.81
1 140.14
Name: MSFT, dtype: float64, 0 238.14
1 232.90
Name: TSLA, dtype: float64]
[0 207.805
1 208.760
Name: AAPL, dtype: float64, 0 140.81
1 140.14
Name: MSFT, dtype: float64, 0 238.14
1 232.90
Name: TSLA, dtype: float64]
[0 207.805
1 208.760
Name: AAPL, dtype: float64, 0 140.81
1 140.14
Name: MSFT, dtype: float64, 0 238.14
1 232.90
Name: TSLA, dtype: float64]

Script:
#import all modules
from yahoofinancials import YahooFinancials
import pandas as pd
import numpy as np
#Stock stickers to get data - declared globally
stocks = ['AAPL','MSFT','TSLA']

#Function to extract data
def stockdata():
    yahoo_financials = YahooFinancials(stocks)
    price = yahoo_financials.get_current_price()
    Open = yahoo_financials.get_open_price()
    return(price, Open)
getval = stockdata()

dF = pd.DataFrame(getval)
df_list = (dF.AAPL, dF.MSFT, dF.TSLA)
list_df = list(df_list)
for u in list_df:
    print(list_df)
Thank you
df_list in your code is a tuple of pd.Series instances. You can access Series elements, e.g. as follows: dF.AAPL.values.
Something like this:

for u in pd.np.hstack(df_list):
    print(u)
should print all items consequently.