美文网首页
Jupyter Notebook使用教程

Jupyter Notebook使用教程

作者: 九章9405 | 来源:发表于2020-04-22 17:14 被阅读0次

Jupyter Notebook使用教程

Jupyter Notebook是一个开源的Web应用程序,允许用户创建和共享包含代码、方程式、可视化和文本的文档。支持超过40种编程语言,包括Python、R、Julia.

1.Jupyter Notebook安装

下载地址:anaconda清华镜像站

1.Jupyter Notebook使用

1.1 打开jJupyter Notebook使用

  • 在终端输入jupyter notebook
  • 点击jupyter notebook快捷方式
打开.png

1.2 Jupyter Notebook创建python

新建文件.png

1.3 Jupyter Notebook运行python程序

运行.png

2.python语法

菜鸟教程:python3教程重点学习以下内容

python语法.png

python安装第三方库:python安装第三方库教程

第三方库.png

3.python连接数据库查询数据

import pandas as pd
from datetime import datetime,timedelta
from sqlalchemy import create_engine
import warnings 
warnings.filterwarnings('ignore')
#连接数据库的方式:引擎名称 = create_engine('引擎://账号:密码@数据库地址:端口/初始数据库')
presto = create_engine('presto://IP地址:28080/hive/default')

#定义空数据
df = pd.DataFrame()
list_e = []        #定义空列表,用户保存执行错误时的参数,方便下次调用
#循环取数
for i in [['2020-04-01','2020-04-20'],['2020-03-01','2020-04-01'],['2020-02-01','2020-03-01'],['2020-01-01','2020-02-01']]:
    #sql,注意{0},{1}为参数,分别对应format()里面的第0个值,第1个值,以此类推.另外,需要注意特殊字符,如like %A%需要改成 like %%A%%
    sql = '''
    SELECT * from src.log_event_flow_whole where thedate >='{0}' and thedate < '{1}' limit 1
    '''.format(i[0],i[1])

    #如果对代码有信心,建议使用try-except的形式执行,避免遇到错误时停止执行
    try:
        df_i = pd.read_sql(sql,presto)   #查询数据,注意presto是引擎名,如果需要查mysql里面的数据,改成mysql即可,sql也用mysql的语法
        df = pd.concat([df,df_i])      #将数据追加到定义的df中
        print(i[0])                #打印i,用于查看进度
    except:
        print(i[0],'错误')            #打印执行错误时对应的i
        list_e.append(i)              #如果执行错误,将i追加到list_e.中
        pass
#保存结果到本地
df.to_excel(r'C:\Users\Administrator\Desktop\查询数据的名称.xlsx')

或者
with pd.ExcelWriter(r'E:\四大数据\this_week_local_data.xlsx', engine='xlsxwriter') as writer:
    df_week_dx_city.to_excel(writer, sheet_name='本周电销城市数据',index=False)
    df_week_all_city.to_excel(writer, sheet_name='本周全部城市数据',index=False)

4.python做分析报告

  • 可视化
    主要用matplotlib、seaborn、plotly、pyecharts等,其中plotly支持控件

可视化神器Plotly_Express详解

  • Markdown
markdown.png
  • 富文本 python富文本输出

    from IPython.display import *
    html = '''
    <table border="1">
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
    </table>
    '''
    display(HTML(html))
    
  • 在jupyter运行以下代码可隐藏jupyter中的代码

    import ipywidgets as widgets
    from IPython.display import display, HTML
    javascript_functions = {False: "hide()", True: "show()"}
    button_descriptions  = {False: "Show code", True: "Hide code"}
    def toggle_code(state):
      output_string = "<script>$(\"div.input\").{}</script>"
      output_args   = (javascript_functions[state],)
      output        = output_string.format(*output_args)
      display(HTML(output))
    def button_action(value):
      state = value.new
      toggle_code(state)
      value.owner.description = button_descriptions[state]
    state = False
    toggle_code(state)
    button = widgets.ToggleButton(state, description = button_descriptions[state])
    button.observe(button_action, "value")
    display(button)
    
  • 导出报告

报告.png

导出pdf比较复杂,可以导出markdown或者latex格式,用其他对应的markdown及latex软件进行渲染

5.python美化excel

openpyxl库:openpyxl库操作excel

6.python发邮件

yagmail库:yagmail库发邮件

相关文章

网友评论

      本文标题:Jupyter Notebook使用教程

      本文链接:https://www.haomeiwen.com/subject/ralwihtx.html