由于它可以在简单的excel表格上工作,所以我制作了一个可以扼杀程序的程序。
我可以简单地描述我的问题:
也是小时分派器的标头,我已经尝试了header = None和parscols = [“”,“”],但标头仍不会加入数据库
即使我避免了丢失的数据,excel工作表的值也不会进入数据库。
这是我的代码:
from src.server.connectToDB import get_sql_conn
import pandas as pd
if __name__ == '__main__':
cursor = get_sql_conn().cursor()
local_files = 'C:\\Users\\dersimw\\Source\Repos\\nordpoolAnalyse\\data\\2011-3.xlsx'
excelFile = pd.ExcelFile(local_files)
ark = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]
for sheets in ark:
df = excelFile.parse(sheets).head(5)
print(df.dropna(axis=1, how='all'))
for key, rows in df.items():
print("# Kolonne: ", "\n")
columnInsertSql = "INSERT INTO DataSets (Hour, BlockBuyNet, BlockSell, RejectedBlockBuy, RejectedBlockSell, NetImports) VALUES"
rowCounter = 0
for column in rows.items():
columnInsertSql += str(column)
if rowCounter != len(list(rows.items())):
columnInsertSql += ", "
rowCounter += 1
print("## SQL: " + columnInsertSql)
cursor.execute(columnInsertSql)
cursor.commit()
这是我打印df.dropna (axis = 1, how = 'all')
时的结果:
小时0 1 ... 21 22 23
0已接受大宗购买112112 ... 227 52 52
1个已接受大宗卖出1573.2 1575.2 ... 1833.8 1728.3 1649.3
2个拒绝购买的商品NaN NaN ... NaN NaN NaN
3次否决销售NaN NaN ... NaN NaN NaN
4个净进口2652.3 2505.9 ... 2932 2962 2897
答案 0 :(得分:0)
看起来您正在尝试删除NA,但是却忘记了放置“ inplace”标志,这就是为什么在打印数据帧时似乎没有任何缺少值的行,而原始Df保持不变的原因。
for sheets in ark:
df = excelFile.parse(sheets).head(5)
df.dropna(axis=1, how='all', inplace=True)
或者,您可以像这样重新声明DF:
for sheets in ark:
df = excelFile.parse(sheets).head(5)
df = df.dropna(axis=1, how='all')
此外,我建议使用Pandas to_sql功能使您的生活更加轻松。 您可以一次上传整个DF,而不是一次上传:
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost:5432/mydatabase')
#Fill in your code to loop through workbooks here
for sheets in ark:
df = excelFile.parse(sheets).head(5)
df = df.dropna(axis=1, how='all')
df.columns =['Hour','BlockBuyNet','BlockSell','RejectedBlockBuy','RejectedBlockSell','NetImports']
df.to_sql('DataSets', con = engine, if_exists='append', index = False)