Python Forum
Yahoo_fin, Pandas: how to convert data table structure in csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yahoo_fin, Pandas: how to convert data table structure in csv file
#1
The below Yahoo_fin script exports 59 stock stats for each ticker to csv. The program is working fine.

import pandas as pd
from yahoo_fin import stock_info as si
import glob
  
stock_list = "MSFT", "AAPL"
 
stats = {}
  
for ticker in stock_list:
  data2 = si.get_stats(ticker)
  data2 = data2.iloc[:,:2]
  data2.columns = ["Attribute", "Recent"]
  stats[ticker] = data2
  
combined2 = pd.concat(stats)
combined2 = combined2.reset_index()
del combined2["level_1"]
combined2.columns = ["Ticker", "Attribute", "Recent"]
  
df = pd.DataFrame(combined2)
df2 = df.drop_duplicates(subset=None, keep="first", inplace=False)
df2.to_csv(r'stats.csv')
The csv output file looks as follows:

Output:
Ticker Attribute Recent 0 MSFT Market Cap 1.84T 1 MSFT Enterprise Value 1.78T . . 58 MSFT Free Cash Flow 34.79B 59 AAPL Market Cap 2.27T 60 AAPL Enterprise Value 2.31T . . 117 AAPL Free Cash Flow 66.89B
Is there are a way to modified the yahoo_fin script so that the output looks like below instead?

Output:
Ticker Market Cap Enterprise Value .. Free Cash Flow MSFT 1.84T 1.78T 34.79B AAPL 2.27T 2.31T 66.89B
Reply


Messages In This Thread
Yahoo_fin, Pandas: how to convert data table structure in csv file - by detlefschmitt - Feb-12-2021, 02:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 877 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,141 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,416 Oct-06-2023, 04:32 PM
Last Post: cubangt
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 758 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Using pyodbc&pandas to load a Table data to df tester_V 3 827 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Hard disk structure like a file selection dialog malonn 2 799 Aug-09-2023, 09:14 PM
Last Post: malonn
  Convert File to Data URL michaelnicol 3 1,175 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 2,534 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  python pandas sql table with header mg24 3 1,970 Dec-08-2022, 08:31 PM
Last Post: Larz60+
  openpyxl convert data to float jacklee26 13 6,022 Nov-19-2022, 11:59 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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