学习笔记--python(6,数据可视,数据库)

一、数据可视图表构建

1、基础柱状图的构建

from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
# 构建柱状图对象
bar=Bar()
bar.add_xaxis(["一月","二月","三月"])
# 通过label_opts=LabelOpts(position="Right"来设置数值标签的显示
bar.add_yaxis("产量",[19,20,45],label_opts=LabelOpts(position="right"))
# 反转xy轴
bar.reversal_axis()
bar.render("基础柱状图.html")

2、基础时间线柱状图的构建

from pyecharts.charts import Bar,Timeline
from pyecharts.globals import ThemeType
from pyecharts.options import LabelOpts

bar1=Bar()
bar1.add_xaxis(["月季","百合","玫瑰"])
bar1.add_yaxis("销量",[30,30,20],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()

bar2=Bar()
bar2.add_xaxis(["月季","百合","玫瑰"])
bar2.add_yaxis("销量",[50,50,50],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()

bar3=Bar()
bar3.add_xaxis(["月季","百合","玫瑰"])
bar3.add_yaxis("销量",[70,60,60],label_opts=LabelOpts(position="right"))
bar3.reversal_axis()
# 构建时间线对象
timeline=Timeline(
    {"theme":ThemeType.CHALK} #设置颜色为主题
)

timeline.add(bar1,"点1")
timeline.add(bar2,"点2")
timeline.add(bar3,"点3")

timeline.add_schema(
    play_interval=1000, # 自动播放的时间间隔,毫秒
    is_timeline_show=True, # 是否在播放时显示时间线
    is_auto_play=True, # 是否自动播放
    is_loop_play=True # 是否循环自动播放
)

timeline.render("基础时间线.html")

 

 主题样式

 二、数据库

1、sql支持注释、大小写不敏感

单行注释:-- 注释内容(--后必须有空格)或者 # 注释内容(#后推荐加上空格)

多行注释:/* 注释内容 */

2、DDL数据定义

create database test charset utf8; # 创建数据库语法:create database 数据库名称 charset utf8;

show databases;# 查看数据库

drop database test;# 删除数据库语法:drop database 数据库名称;

select database();# 查看当前使用的数据库

use test;# 使用数据库语法:use 数据库名称

 

use sys;
show tables;
# 删除表 drop table 表名称;
# 删除表 drop table if exists 表名称;

create table student(
    id int,
    num int,
    name varchar(20),
    class int
);

3、DML

数据插入

基础语法:insert into表[(列1,列2,......,列N)] values(值1,值2,…....,值N)[,(值1,值2, ……...,值N),…...,(值1,值2,....,值N)] 

use sys;
create table student(
    id int,
    num int,
    name varchar(20),
    class int
);

insert into student(id,num,name,class) values(1,21,'yi',2),(2,22,'io',1),(3,23,'nj',4);

# 数据删除 基础语法:delete from 表名称 where 条件判断(列 操作符 值)
delete from student where id>2;

# 数据更新 基础语法:update 表名 set 列=值 where 条件判断;
update student set name='yi' where id=2;
# 更新全局
update student set id=1;

4、DQL

# 基础查询:select 字段列表|* from 表
select id,num,name from student;
# 过滤查询:select 字段列表|* from 表 where 条件判断
select id,num,name from student where id<2;

# 分组聚合:select 字段|聚合函数 from 表 where 条件 group by 列
select id ,max(num) from student group by id;

# 排序分页
/*
select 列|聚合函数|* from 表
where ...
group by ...
order by ...[asc|desc]
limit n[,m]
*/

select num, count(*) from student where num>2 group by num
order by age limit 2

 聚合函数有:

sum(列)求和

avg(列)求平均值

min(列)求最小值

max(列)求最大值

count(列|*)求数量

 执行顺序:from>where>group by 和聚合函数>select>order by>limit

5、python操作MySQL基础使用

from pymysql import Connection

conn = Connection(
    host='localhost',
    port=3306,
    user='root',
    password='041129hyz'
)
print(conn.get_server_info())
# 执行非查询性质sql 获取到游标对象
cursor = conn.cursor() 
# 选择数据库
conn.select_db("test")
# 执行sql
# cursor.execute("create table test_pymysql(id int);")
# cursor.execute("create table test_pymysql(id int,info varchar(10))")
cursor.execute("select * from student")
result=cursor.fetchall()


print(result)
conn.close()
"""
# 插入数据
conn.select_db("sys")
cursor.execute("insert into student values (4,5,'op',98)")
conn.comit()# 确认
"""

若插入数据不想手动commit确认,可在构建链接对象时设置自动commit属性 ,即在password下一行加上 autocommit=True# 自动提交

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值