Python Forum
Creating A List of DataFrames & Manipulating Columns in Each DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating A List of DataFrames & Manipulating Columns in Each DataFrame
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to most effectively unpack list of name-value pair dictionaries in a dataframe? zlim 1 613 Nov-07-2023, 10:56 PM
Last Post: zlim
  How to add columns to polars dataframe sayyedkamran 1 1,689 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 776 Oct-03-2023, 09:29 PM
Last Post: flash77
  Creating a Dataframe from Zenodo zip file with multiple CSVs about Spotify man0s 0 1,326 Apr-26-2022, 01:45 PM
Last Post: man0s
  Convert several columns to int in dataframe Krayna 2 2,362 May-21-2021, 08:55 AM
Last Post: Krayna
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 2,238 May-17-2021, 07:02 AM
Last Post: lorensa74
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,572 Jan-27-2021, 10:59 AM
Last Post: epsilon
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,915 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 2,320 Dec-17-2020, 01:08 PM
Last Post: palladium
  Adapting a dataframe to the some of columns flyway 2 2,032 Aug-12-2020, 07:21 AM
Last Post: flyway

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020