如何在excel中存储python脚本的aws athena输出?
我使用python脚本和pyathena库从aws athena进行查询,并以表的形式获得正确的输出 现在的问题是我想将输出存储在excel中 有谁能建议我,使用python脚本如何在Excel中存储输出 以下是我在aws athena中用于查询的代码:如何在excel中存储python脚本的aws athena输出?,python,pandas,amazon-web-services,matplotlib,amazon-athena,Python,Pandas,Amazon Web Services,Matplotlib,Amazon Athena,我使用python脚本和pyathena库从aws athena进行查询,并以表的形式获得正确的输出 现在的问题是我想将输出存储在excel中 有谁能建议我,使用python脚本如何在Excel中存储输出 以下是我在aws athena中用于查询的代码: from pyathena import connect import os import pandas as pd %matplotlib inline conn = connect(aws_access_key_id='*****',
from pyathena import connect
import os
import pandas as pd
%matplotlib inline
conn = connect(aws_access_key_id='*****',
aws_secret_access_key='*****',
s3_staging_dir='s3://****/',
region_name='us-west-1')
cursor = conn.cursor()
%time cursor.execute("SELECT * from my_table;")
提前感谢…Excel的输出不限于创建xlsx文件,您也可以将其写入csv并让Excel加载csv文件 可以使用以下方法创建多张图纸:
from pandas import ExcelWriter
def save_xls(list_dfs, dfs_names, xls_path):
with ExcelWriter(xls_path) as writer:
for df,name in zip(list_dfs, dfs_names):
df.to_excel(writer,name)
writer.save()
然后,您可以通过对数据进行一些转换来调用该函数,例如透视表甚至颜色:
save_xls(
(raw.style.format("{:,.0f}"),
actual_table.style.format("{:,.0f}"),
diff_table.style.applymap(_color_red_or_green).format("{:,.0f}"),
ratio_table.style.applymap(_color_red_yellow_or_green).format("{:.3f}")),
('Raw',
'Actuals',
'Diff',
'Ratio'),
results_with_format.xlsx')
例如,根据单元格的值使用三种颜色进行格式化:
def _color_red_yellow_or_green(val):
color = 'red' if val > 0.1 else 'yellow' if val > 0.05 else 'green'
return 'background-color: %s' % color
您可以使用pandas在excel中查询和保存数据:
data = pd.read_sql("SELECT * from my_table;",conn)
data.to_excel('data.xlsx')
根据在工作表中插入数据的精度,您可以签出OpenPyXl- 当我需要将Athena结果插入工作簿中的特定单元格和/或工作表时,我会使用它。当我需要比显示结果表更精确的时候。您可以引用单个单元格,如工作表['A53']=12345