【问题标题】:PyMySQL - getting column data from dictionary by fetchall inPyMySQL - 通过 fetchall 从字典中获取列数据
【发布时间】:2018-08-10 00:40:15
【问题描述】:

使用 PyMySQL python 3.6.3 版本,得到一个DictCursor,然后是fetchall()。我得到了所有数据,.description 说: (('recdate', 12, None, 19, 19, 0, False), ('outdoorhumidity', 246, None, 9, 9, 3, True), ('outdoortemperature', 246, None, 9, 9, 3, True)).

打印我得到的行,f.ex:

2005-12-31 23:12:00    89.000   -6.667
2005-12-31 23:13:00    89.000   -6.667
2005-12-31 23:15:00    89.000   -6.650
2005-12-31 23:16:00    89.000   -6.650
2005-12-31 23:17:00    89.000   -6.640

注意丢失的一分钟 ...23:14:00 - 但我这样做是为了更大的丢失数据插补项目。所以这是围绕缺失数据的样本数据。通过字典,我想以最好的方式获得不完整的时间序列以及 f ex 3rd 列 - 简单易读的代码?在每种情况下我都必须知道有多少行吗?

import pymysql

dbServerName = "127.0.0.1"
dbUser = "root"
dbPassword = "mypwd"
dbName = "dbname"
charSet = "utf8"
cursorType = pymysql.cursors.DictCursor

connectionObject = pymysql.connect(host=dbServerName, user=dbUser, password=dbPassword,
                                     db=dbName, charset=charSet,cursorclass=cursorType)

try:
    cursorObject = connectionObject.cursor()                                     

    sqlQuery = "SELECT recdate, outdoorhumidity, outdoortemperature FROM mytable WHERE recdate BETWEEN '2005-12-31 23:12:00' AND '2005-12-31 23:17:00';"


    cursorObject.execute(sqlQuery)

    #Fetch all the rows - within the cursor? Can this be done?
    rows = cursorObject.fetchall()

    print(cursorObject.description)

    for row in rows:
        print(row["recdate"], row["outdoorhumidity"], row["outdoortemperature"])   

except Exception as e:
    print("Exeception occured:{}".format(e))

finally:
    cursorObject.close()
    connectionObject.close()

【问题讨论】:

  • 请尝试从您的项目中添加一些代码,以便我们了解上下文。此外,其中大部分可以使用内联格式。见how to ask
  • “f ex 3rd column”是指应该在第三位(索引 2)的缺失列吗?
  • 我想在 ML 中将“recdate”和“outdoortemperature”视为“一对”,以便稍后在表格中获得新的插补值
  • “对”是什么意思?你想要两个值的字典,使用recdateoutdoortemperature 作为键?
  • spikepaz - 我真的不知道 - 我想要一些东西,尽可能简单地结合我放入一个新的“记录”,只有 recdate -即日期时间和所有其他字段为空,即 NaN,其中 ML 可以在第 3 列中的 NaN(s) 之前和之后看到周围的数据,并计算要插补的数据 - 插入到数据库中。缺失的记录必须用 7 个基础数据的计算值替换,然后是一系列计算字段。 IE。 temp hum press 将成为同一行/记录中的露点。

标签: python-3.x pymysql fetchall


【解决方案1】:

我想要实现的目标在 pandas、sqlalchemy、datetime 和 time 中使用起来要简单得多。并且还就引用 Pandas DataFrames 的不同方式发表了评论。

有些事情对我来说有点特殊,但对其他人来说可能很有趣,尤其是在使用 .iloc 和 .loc 以及类型转换处理时间方面。

# Python version
# '3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 
       bit (AMD64)]'
# Running Spyder IDE version 3.2.6
# PANDAS VERSION '0.22.0'enter code here
import pandas as pd
import time
from datetime import datetime
from sqlalchemy import create_engine



def GetSqlData(begdatetime,enddatetime):
    temp_df = pd.read_sql_query("""SELECT recdate, outdoortemperature
                   FROM osterasen 
                   WHERE recdate BETWEEN 
                   %(c1)s
                   AND
                   %(c2)s """,
                   engine,
                   params={'c1': begdatetime, 'c2': enddatetime}
                   )
    temp_df['recdate'] = temp_df['recdate'].astype('datetime64[ns]')
    return temp_df

iso_datetimeformat = "%Y-%m-%d %H:%M:%S"

# BEGIN - CREATE MOCK MISSING DATES
# would have to come from the WeatherData.missings table
print("Step 0.0: ")
print("     Get missing data info from weatherdata.missings...")

num_of_missings=21
range_good_m = num_of_missings * 2
print(range_good_m)
range_good_s = range_good_m * 60
print(range_good_s)
a_complete_end_str = '2005-09-24 09:09:00'

b_complete_beg_str = '2005-09-24 09:31:00'

a_complete_end = time.mktime(datetime.strptime(a_complete_end_str, 
    iso_datetimeformat ).timetuple())
print("a_complete_end: ", a_complete_end)

b_complete_beg = time.mktime(datetime.strptime(b_complete_beg_str, i 
iso_datetimeformat ).timetuple())
print("b_complete_beg: ", b_complete_beg)

a_complete_beg = a_complete_end - range_good_s 
print("a_complete_beg: ", a_complete_beg)

b_complete_end = b_complete_beg + range_good_s
print("b_complete_end: ", b_complete_end)
print("b_complete_end: type: ", type(b_complete_end))

m_missing_beg = a_complete_end + 60
m_missing_end = b_complete_beg - 60
m_missing_beg_str = str(datetime.fromtimestamp(m_missing_beg))
m_missing_end_str = str(datetime.fromtimestamp(m_missing_end))

a_complete_beg_str = str(datetime.fromtimestamp(a_complete_beg ))
b_complete_end_str = str(datetime.fromtimestamp(b_complete_end ))

# Print out the ranges
print(a_complete_beg_str)
print(a_complete_end_str)
print(m_missing_beg_str)
print(m_missing_end_str)
print(b_complete_beg_str)
print(b_complete_end_str)

# END   - CREATE MOCK MISSING DATES

connection_str = 
    'mysql+pymysql://root:mypassword@127.0.0.1:3306/weatherdata'

engine = create_engine(connection_str,encoding='utf8')




print("Step 1.0: ")
df_a = GetSqlData( a_complete_beg_str, a_complete_end_str )
print(type(df_a),"   ", type(df_a.head))
print(df_a)
x1 = df_a.loc[5, 'outdoortemperature']
y1 = df_a.loc[5, 'recdate']
print("x1 = ", x1, type(x1))
print("y1 = ", y1, type(y1))



print("Step 2.0: ")
df_b = GetSqlData( b_complete_beg_str, b_complete_end_str )
print(type(df_b),"   ", type(df_b.head))
print(df_b)
x2 = df_b.loc[5, 'outdoortemperature']
y2 = df_b.loc[5, 'recdate']
print("x2 = ", x2, type(x2))
print("y2 = ", y2, type(y2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多