Python Forum

Full Version: change dataframe header with 2 rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
(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())
(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