{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"
Table of Contents
\n",
""
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# 设置全部行输出\n",
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# python连接mysql数据库"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# 导包\n",
"import pymysql \n",
"\n",
"# 创建连接对象\n",
"mydb = pymysql.connect( \n",
" host=\"localhost\", # 数据库主机地址\n",
" user=\"root\", # 数据库用户名\n",
" password=\"1234\" , # 数据库密码\n",
" database=None, # 可以指定连接某个数据库\n",
" port=3306, # 端口号, 默认是3306\n",
" charset='utf8' # 使用的编码\n",
" ) \n",
"\n",
"# 创建游标对象, 这里创建的游标是 可以返回带字段名的字典, 比较好用\n",
"mycursor = mydb.cursor(cursor=pymysql.cursors.DictCursor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SQL"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 库操作"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 查看数据库"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Database | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" information_schema | \n",
"
\n",
" \n",
" 1 | \n",
" 学生管理系统 | \n",
"
\n",
" \n",
" 2 | \n",
" mysql | \n",
"
\n",
" \n",
" 3 | \n",
" pandas_to_sql | \n",
"
\n",
" \n",
" 4 | \n",
" performance_schema | \n",
"
\n",
" \n",
" 5 | \n",
" python | \n",
"
\n",
" \n",
" 6 | \n",
" school | \n",
"
\n",
" \n",
" 7 | \n",
" sys | \n",
"
\n",
" \n",
" 8 | \n",
" test | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Database\n",
"0 information_schema\n",
"1 学生管理系统\n",
"2 mysql\n",
"3 pandas_to_sql\n",
"4 performance_schema\n",
"5 python\n",
"6 school\n",
"7 sys\n",
"8 test"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 通过游标对象的execute方法执行sql语句\n",
"mycursor.execute('''\n",
" show databases;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(('Database', 253, None, 64, 64, 0, False),)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.description # 列名是'Database\n",
"\n",
"# 这里也可以查询出列名"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 创建数据库 \n",
"新创建的数据库名字不能和sql中的关键字一样, 也不能和已经存在的数据库重名"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" create database 数据库;\n",
"''')\n",
"pd.DataFrame(mycursor) # 创建数据库不会有返回结果"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Database | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" information_schema | \n",
"
\n",
" \n",
" 1 | \n",
" 学生管理系统 | \n",
"
\n",
" \n",
" 2 | \n",
" 数据库 | \n",
"
\n",
" \n",
" 3 | \n",
" mysql | \n",
"
\n",
" \n",
" 4 | \n",
" pandas_to_sql | \n",
"
\n",
" \n",
" 5 | \n",
" performance_schema | \n",
"
\n",
" \n",
" 6 | \n",
" python | \n",
"
\n",
" \n",
" 7 | \n",
" school | \n",
"
\n",
" \n",
" 8 | \n",
" sys | \n",
"
\n",
" \n",
" 9 | \n",
" test | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Database\n",
"0 information_schema\n",
"1 学生管理系统\n",
"2 数据库\n",
"3 mysql\n",
"4 pandas_to_sql\n",
"5 performance_schema\n",
"6 python\n",
"7 school\n",
"8 sys\n",
"9 test"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 再次查询以便就有了 数据库\n",
"mycursor.execute('''\n",
" show databases;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 删除数据库"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" drop database 数据库;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 查看已经创建好的数据库 \n",
"返回该数据的一些信息"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Database | \n",
" Create Database | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 学生管理系统 | \n",
" CREATE DATABASE `学生管理系统` /*!40100 DEFAULT CHAR... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Database Create Database\n",
"0 学生管理系统 CREATE DATABASE `学生管理系统` /*!40100 DEFAULT CHAR..."
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" show create database 学生管理系统;\n",
"''')\n",
"a = pd.DataFrame(mycursor)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([['学生管理系统',\n",
" 'CREATE DATABASE `学生管理系统` /*!40100 DEFAULT CHARACTER SET utf8 */']],\n",
" dtype=object)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.values # 可以看出该数据库编码是utf8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 修改数据库编码"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" alter database 学生管理系统 character set gbk;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Database | \n",
" Create Database | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 学生管理系统 | \n",
" CREATE DATABASE `学生管理系统` /*!40100 DEFAULT CHAR... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Database Create Database\n",
"0 学生管理系统 CREATE DATABASE `学生管理系统` /*!40100 DEFAULT CHAR..."
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" show create database 学生管理系统;\n",
"''')\n",
"a = pd.DataFrame(mycursor)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([['学生管理系统',\n",
" 'CREATE DATABASE `学生管理系统` /*!40100 DEFAULT CHARACTER SET gbk */']],\n",
" dtype=object)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.values # 编码已经修改成gbk"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 使用某个数据库"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" use 学生管理系统;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 表操作对应DataFrame的属性和方法"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" use school;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Tables_in_school | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" stu | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Tables_in_school\n",
"0 stu"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" show tables;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#使用包含列表的字典创建DataFrame时,各个列表内元素个数必须一致\n",
"data = {'人物姓名':['小明','小高','小婉'], #默认字典的键为dataframe的字段名,不设定索引的时候,自动给出默认索引,从0到len(list)-1\n",
" '语文':[88,90,67],\n",
" '数学':[60,40,100],\n",
" '英语':[78, 89, 87]\n",
" }\n",
"data = pd.DataFrame(data)\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# 创建数据库引擎\n",
"from sqlalchemy import create_engine\n",
"engine = create_engine(\"mysql+mysqlconnector://root:1234@localhost:3306/school?charset=utf8\")\n",
"\n",
"# 这里需要先安装:pip install mysql-connector-python -i https://pypi.douban.com/simple\n",
"# 这里如果使用pymysql会有一点问题, 建议就使用mysql-connector-python"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"data.to_sql(name='data', con=engine, index=False)\n",
"\n",
"# name是表名\n",
"# con是数据库引擎\n",
"# index=False忽略原行索引"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.head(2)近似操作"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head(2)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select * from data\n",
" limit 2;\n",
"''')\n",
"pd.DataFrame(mycursor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.shape的近似操作"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 行数 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 行数\n",
"0 3"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select count(*) as 行数 from data;\n",
"\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 列数 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 4 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 列数\n",
"0 4"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select count(COLUMN_NAME) as 列数 from information_schema.columns \n",
" where table_schema='school' and table_name='data';\n",
"\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.index的近似操作-无 \n",
"数据库中是没有index的"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.columns的近似操作"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 列名 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 人物姓名 | \n",
"
\n",
" \n",
" 1 | \n",
" 语文 | \n",
"
\n",
" \n",
" 2 | \n",
" 数学 | \n",
"
\n",
" \n",
" 3 | \n",
" 英语 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 列名\n",
"0 人物姓名\n",
"1 语文\n",
"2 数学\n",
"3 英语"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select COLUMN_NAME as 列名 from information_schema.columns \n",
" where table_schema='school' and table_name='data';\n",
"\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.info()近似操作"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Field | \n",
" Type | \n",
" Null | \n",
" Key | \n",
" Default | \n",
" Extra | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 人物姓名 | \n",
" text | \n",
" YES | \n",
" | \n",
" None | \n",
" | \n",
"
\n",
" \n",
" 1 | \n",
" 语文 | \n",
" bigint(20) | \n",
" YES | \n",
" | \n",
" None | \n",
" | \n",
"
\n",
" \n",
" 2 | \n",
" 数学 | \n",
" bigint(20) | \n",
" YES | \n",
" | \n",
" None | \n",
" | \n",
"
\n",
" \n",
" 3 | \n",
" 英语 | \n",
" bigint(20) | \n",
" YES | \n",
" | \n",
" None | \n",
" | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Field Type Null Key Default Extra\n",
"0 人物姓名 text YES None \n",
"1 语文 bigint(20) YES None \n",
"2 数学 bigint(20) YES None \n",
"3 英语 bigint(20) YES None "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" desc data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.describe()的近似操作"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" count | \n",
" mean | \n",
" std | \n",
" min | \n",
" 25% | \n",
" 50% | \n",
" 75% | \n",
" max | \n",
"
\n",
" \n",
" \n",
" \n",
" 语文 | \n",
" 3.0 | \n",
" 81.666667 | \n",
" 12.741010 | \n",
" 67.0 | \n",
" 77.5 | \n",
" 88.0 | \n",
" 89.0 | \n",
" 90.0 | \n",
"
\n",
" \n",
" 数学 | \n",
" 3.0 | \n",
" 66.666667 | \n",
" 30.550505 | \n",
" 40.0 | \n",
" 50.0 | \n",
" 60.0 | \n",
" 80.0 | \n",
" 100.0 | \n",
"
\n",
" \n",
" 英语 | \n",
" 3.0 | \n",
" 84.666667 | \n",
" 5.859465 | \n",
" 78.0 | \n",
" 82.5 | \n",
" 87.0 | \n",
" 88.0 | \n",
" 89.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" count mean std min 25% 50% 75% max\n",
"语文 3.0 81.666667 12.741010 67.0 77.5 88.0 89.0 90.0\n",
"数学 3.0 66.666667 30.550505 40.0 50.0 60.0 80.0 100.0\n",
"英语 3.0 84.666667 5.859465 78.0 82.5 87.0 88.0 89.0"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.describe().T"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" count | \n",
" mean | \n",
" min | \n",
" max | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 3 | \n",
" 81.6667 | \n",
" 67 | \n",
" 90 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" count mean min max\n",
"0 3 81.6667 67 90"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select \n",
" count(语文) count, avg(语文) mean, min(语文) min , max(语文) max\n",
" from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.sort_values()近似操作"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"2 小婉 67 100 87\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.sort_values('数学', ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
" 1 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 2 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小婉 67 100 87\n",
"1 小明 88 60 78\n",
"2 小高 90 40 89"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select * from data\n",
" order by 数学 desc;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.drop_duplicates()近似操作"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.drop_duplicates()"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select distinct* from data;\n",
" \n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### df.isna()近似操作"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [人物姓名, 语文, 数学, 英语]\n",
"Index: []"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[data.数学.isna()]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select * from data\n",
" where 数学 is null\n",
" ;\n",
" \n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DataFrame对象的列操作和行操作"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 增加列"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语\n",
"0 小明 88 60 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 增加一列字段 性别\n",
"mycursor.execute('''\n",
" alter table data add 性别 varchar(10);\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
" 性别 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
" None | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
" None | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
" None | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语 性别\n",
"0 小明 88 60 78 None\n",
"1 小高 90 40 89 None\n",
"2 小婉 67 100 87 None"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 给 性别 传入值\n",
"mycursor.execute('''\n",
" update data set 性别='男';\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
" 性别 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
" 男 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
" 男 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
" 男 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语 性别\n",
"0 小明 88 60 78 男\n",
"1 小高 90 40 89 男\n",
"2 小婉 67 100 87 男"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 给 性别 传入值\n",
"mycursor.execute('''\n",
" update data set 性别='女'\n",
" where 人物姓名='小婉'\n",
" ;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
" 性别 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
" 男 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
" 男 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
" 女 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 语文 数学 英语 性别\n",
"0 小明 88 60 78 男\n",
"1 小高 90 40 89 男\n",
"2 小婉 67 100 87 女"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 移动列"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 把 性别 移动到 人物姓名 之后\n",
"mycursor.execute('''\n",
" alter table data modify 性别 varchar(10) after 人物姓名;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 人物姓名 | \n",
" 性别 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 男 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 男 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 女 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 人物姓名 性别 语文 数学 英语\n",
"0 小明 男 88 60 78\n",
"1 小高 男 90 40 89\n",
"2 小婉 女 67 100 87"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 修改列"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 修改列的名"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: []\n",
"Index: []"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 把 人物姓名 修改为 姓名\n",
"mycursor.execute('''\n",
" alter table data change 人物姓名 姓名 varchar(10);\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 姓名 | \n",
" 性别 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 男 | \n",
" 88 | \n",
" 60 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 男 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 女 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 姓名 性别 语文 数学 英语\n",
"0 小明 男 88 60 78\n",
"1 小高 男 90 40 89\n",
"2 小婉 女 67 100 87"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 修改列的值"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 将小明的数学改为100\n",
"mycursor.execute('''\n",
" update data set 数学=100\n",
" where 姓名='小明';\n",
"\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 姓名 | \n",
" 性别 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 男 | \n",
" 88 | \n",
" 100 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 男 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 女 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 姓名 性别 语文 数学 英语\n",
"0 小明 男 88 100 78\n",
"1 小高 男 90 40 89\n",
"2 小婉 女 67 100 87"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 删除列"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" alter table data drop 性别; \n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 姓名 | \n",
" 语文 | \n",
" 数学 | \n",
" 英语 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 小明 | \n",
" 88 | \n",
" 100 | \n",
" 78 | \n",
"
\n",
" \n",
" 1 | \n",
" 小高 | \n",
" 90 | \n",
" 40 | \n",
" 89 | \n",
"
\n",
" \n",
" 2 | \n",
" 小婉 | \n",
" 67 | \n",
" 100 | \n",
" 87 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 姓名 语文 数学 英语\n",
"0 小明 88 100 78\n",
"1 小高 90 40 89\n",
"2 小婉 67 100 87"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycursor.execute('''\n",
" select*from data;\n",
"''')\n",
"pd.DataFrame(mycursor) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "210.323px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}