将第一行添加到标题

时间:2019-07-08 10:33:42

标签: python pandas dataframe

我从假定为“ pandas”格式的API(alpha_vantage)获取数据。 我用此数据创建一个pd.DataFrame。在此之后,应该将Table / DataFrame插入我的MYSQL数据库中以创建内容并将该DataFrame与另一个进行比较,等等...

但是标头中的值位于标头AND中的第一行。 当我重命名列时,数据框中的值位于NaN。

您是否有任何解决此DataFrame的想法?

open    high    low close   volume
date                    
2019-07-05 14:21:00 NaN NaN NaN NaN NaN
2019-07-05 14:22:00 NaN NaN NaN NaN NaN
2019-07-05 14:23:00 NaN NaN NaN NaN NaN
2019-07-05 14:24:00 NaN NaN NaN

这是导入之后的DataFrame:

    1. open 2. high 3. low  4. close    5. volume
date                    
2019-07-05 14:21:00 136.890 136.9300    136.87  136.9300    17242.0
2019-07-05 14:22:00 136.920 136.9200    136.90  136.9000    15627.0
2019-07-05 14:23:00 136.905 136.9149    136.89  136.9149    21360.0
2019-07-05 14:24:00 136.920 136.9400    136.90  136.9000    14474.0
2019-07-05 14:25:00 136.905 136.9400    136.90  136.9350    10476.0

此作业的实际代码:

#db-connection#
import pandas as pd
import mysql.connector
mydb = mysql.connector.connect(host="xxx",port="xxx",user="xxx",passwd="xxx",database="traiding")
mycursor = mydb.cursor()


#data#
from alpha_vantage.timeseries import TimeSeries
import csv
ts = TimeSeries(key='xxxx', output_format='pandas')
data, meta_data = ts.get_intraday(symbol='MSFT',interval='1min', outputsize='compact')

df_data=pd.DataFrame(data=data) #, columns=(['open','high','low','close','volume']))





#write data in db#


#mydb.commit()

print("1 record inserted, ID:", mycursor.lastrowid)

mycursor.close()
mydb.close()
print("Connection Closed")

df_data.head()

0 个答案:

没有答案