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
  Create dataframe from the unique data of two dataframes Calab 6 796 Mar-02-2025, 01:51 PM
Last Post: Pedroski55
  Find duplicates in a pandas dataframe list column on other rows Calab 2 1,963 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,558 Aug-26-2024, 04:52 PM
Last Post: Calab
  Creating release on github remote repo with file list Unkovic 1 944 Jun-20-2024, 04:55 AM
Last Post: rodiongork
  How to most effectively unpack list of name-value pair dictionaries in a dataframe? zlim 1 2,807 Nov-07-2023, 10:56 PM
Last Post: zlim
  How to add columns to polars dataframe sayyedkamran 1 3,098 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 2,089 Oct-03-2023, 09:29 PM
Last Post: flash77
  Creating a Dataframe from Zenodo zip file with multiple CSVs about Spotify man0s 0 1,809 Apr-26-2022, 01:45 PM
Last Post: man0s
  Convert several columns to int in dataframe Krayna 2 3,238 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,972 May-17-2021, 07:02 AM
Last Post: lorensa74

Forum Jump:

User Panel Messages

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