Bottom Page

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
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Difference of two columns in Pandas dataframe zinho 2 447 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 300 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 314 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 306 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  How to highlight dataframe columns SriRajesh 1 298 Feb-28-2020, 11:02 PM
Last Post: Marbelous
  Does the order of columns in the DataFrame matter? new_to_python 11 658 Feb-17-2020, 12:32 AM
Last Post: scidam
  Merging two DataFrames based on indexes from two other DataFrames lucinda_rigeitti 0 215 Jan-16-2020, 08:36 PM
Last Post: lucinda_rigeitti
  manipulating a dataframe - pandas nsx200 2 419 Nov-14-2019, 10:38 AM
Last Post: nsx200
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 678 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  How modify the DataFrame columns SriRajesh 2 455 Sep-12-2019, 03:14 PM
Last Post: SriRajesh

Forum Jump:


Users browsing this thread: 1 Guest(s)