Python Forum
change dataframe header with 2 rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change dataframe header with 2 rows
#1
Hello, I am new to programming, have some questions , thanks ! Big Grin

I download stock quote from yahoo and return two dataframes, by 2 methods

data1 = yf.download('msft ibm v',start ='2020-10-5')

data2 = yf.download('msft ibm v' , group_by ='ticker',start ='2020-10-5')

both of them have two rows of header


question 1 :how to change data1 to data2 structure or change data2 to data1 structure?
question 2 :how to store the dataframe to csv or db , with 2 rows of header?

#pip install yfinance
import yfinance as yf
data1 = yf.download('msft ibm v',start ='2020-10-5')
data2 = yf.download('msft ibm v' , group_by ='ticker',start ='2020-10-5')
data1.tail(5)
data2.tail(5
Dance
Reply
#2
(Oct-28-2020, 10:22 AM)tonycat Wrote: Hello, I am new to programming, have some questions , thanks ! Big Grin

I download stock quote from yahoo and return two dataframes, by 2 methods

data1 = yf.download('msft ibm v',start ='2020-10-5')

data2 = yf.download('msft ibm v' , group_by ='ticker',start ='2020-10-5')

both of them have two rows of header


question 1 :how to change data1 to data2 structure or change data2 to data1 structure?
question 2 :how to store the dataframe to csv or db , with 2 rows of header?

#pip install yfinance
import yfinance as yf
data1 = yf.download('msft ibm v',start ='2020-10-5')
data2 = yf.download('msft ibm v' , group_by ='ticker',start ='2020-10-5')
data1.tail(5)
data2.tail(5
Dance

Hello,
for start, you can try to write data frames converted to string into text files and examine how columns are organized. Why writing to db? What do you want to actually do with these files?
Start with this code:
import yfinance as yf
data1 = yf.download('msft ibm v', start='2020-10-5')
data2 = yf.download('msft ibm v', group_by='ticker', start='2020-10-5')
data1.tail(5)
data2.tail(5)

with open('Data1.txt', 'w') as data1_f:
    data1_f.write(data1.to_string())

with open('Data2.txt', 'w') as data2_f:
    data2_f.write(data2.to_string())
tonycat likes this post
Reply
#3
(Oct-28-2020, 05:32 PM)Askic Wrote:
(Oct-28-2020, 10:22 AM)tonycat Wrote: Hello, I am new to programming, have some questions , thanks ! Big Grin

I download stock quote from yahoo and return two dataframes, by 2 methods

data1 = yf.download('msft ibm v',start ='2020-10-5')

data2 = yf.download('msft ibm v' , group_by ='ticker',start ='2020-10-5')

both of them have two rows of header


question 1 :how to change data1 to data2 structure or change data2 to data1 structure?
question 2 :how to store the dataframe to csv or db , with 2 rows of header?

#pip install yfinance
import yfinance as yf
data1 = yf.download('msft ibm v',start ='2020-10-5')
data2 = yf.download('msft ibm v' , group_by ='ticker',start ='2020-10-5')
data1.tail(5)
data2.tail(5
Dance

Hello,
for start, you can try to write data frames converted to string into text files and examine how columns are organized. Why writing to db? What do you want to actually do with these files?
Start with this code:
import yfinance as yf
data1 = yf.download('msft ibm v', start='2020-10-5')
data2 = yf.download('msft ibm v', group_by='ticker', start='2020-10-5')
data1.tail(5)
data2.tail(5)

with open('Data1.txt', 'w') as data1_f:
    data1_f.write(data1.to_string())

with open('Data2.txt', 'w') as data2_f:
    data2_f.write(data2.to_string())

Big Grin thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  How to add a few empty rows into a pandas dataframe python_newbie09 2 16,280 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dataframe Rows Sorting stranger14u 1 2,551 Dec-17-2018, 11:47 PM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,427 Oct-18-2018, 09:33 PM
Last Post: volcano63
  Add rows in dataframe arya_starc 1 2,346 Oct-03-2018, 08:02 AM
Last Post: volcano63
  Get rows with same value from dataframe of particular columns angelwings 1 2,708 Apr-11-2018, 02:40 AM
Last Post: scidam
  Dropping rows in a dataframe sobrio1 0 2,603 Dec-03-2017, 11:15 PM
Last Post: sobrio1
  Stack dataframe columns into rows klllmmm 0 3,007 Sep-03-2017, 02:26 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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