【Python】pandas Week 8 - 1:环境搭建与基础概念
一、学习目标搭建Python 环境理解 DataFrame 和 Series学会读取和查看数据二、Pandas vs SQL 语法对照SQL概念pandas对应学习重点SELECT * FROM tabledf或df.head()查看数据SELECT col1, col2df[[col1, col2]]选择列WHEREdf[df[col] 100]条件筛选GROUP BYdf.groupby(col)分组聚合JOINpd.merge(df1, df2)表关联ORDER BYdf.sort_values(col)排序SUM/AVG/COUNTdf[col].sum()聚合函数CASE WHENdf.apply()或np.where()条件判断三、学习内容1、环境搭建# 安装 Anaconda 或 pip 安装 pip install pandas numpy sqlalchemy pymysql jupyter # 启动 jupyter jupyter notebook2、第一个 pandas 程序import pandas as pd # 创建 DataFrame 一个类似 SQL 的表 df pd.DataFrame({ product_id: [1, 2, 3, 4, 5], product_name: [产品A, 产品B, 产品C, 产品D, 产品E], price: [100, 200, 150, 300, 250], quantity: [10, 20, 15, 5, 8] }) # 查看数据类似 SQL 的 SELECT print(查看全部数据\\n,df) # 查看全部数据 print(查看前3行数据LIMIT 3\\n, df.head(3)) # print(查看最后2行数据\\n, df.tail(2)) # 最后2行 print(查看维度行数列数\\n, df.shape) # 维 print(查看数据信息DESC\\n, df.info()) # 数据信 print(查看统计描述\\n, df.describe()) # 统计描述3、读取外部数据import pandas as pd from sqlalchemy import create_engine # 写入数据 df.to_csv(products.csv, indexFalse) # indexFalse 不写入索引 df.to_excel(products.xlsx, indexFalse) # 读取数据 df1 pd.read_csv(products.csv) # 读取 CSV 文件 df2 pd.read_excel(products.xlsx) # 读取 Excel 文件 print(df1) print(df2) # 读取 SQL engine create_engine( mysqlpymysql://10.200.13.59:9031/ads?charsetutf8, connect_args{ user: garciashan, password: garciashan205324, port: 9031, } ) df pd.read_sql(SELECT * FROM ads.ads_dim_site, engine) print(df) # 保存数据 df.to_csv(output.csv, indexFalse) df.to_excel(output.xlsx, indexFalse)四、本周练习练习1创建一个包含以下列的 DataFrame员工 ID 、姓名、部门、工资、入职日期至少10条数据用 head() 、tail()、info()、describe() 查看from sqlalchemy import create_engine import pandas as pd df pd.DataFrame({ 员工ID: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 姓名: [Sasa, Ami, Kimi, Jason, Sara, Tom, Jim, Phill, Zoe, Mike], 部门: [HR, HR, IT, IT, FIN, FIN, OPS, OPS, OCT, OCT], 工资: [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000], 入职日期: [2022-01-01, 2022-02-01, 2022-03-01, 2022-04-01, 2022-05-01, 2022-06-01, 2022-07-01, 2022-08-01, 2022-09-01, 2022-10-01], }) print(df.head():\\n, df.head()) print(df.tail():\\n,df.tail()) print(df.info():\\n, df.info()) print(df.describe():\\n, df.describe())练习2从你的工作数据库读取一张表到 pandasfrom sqlalchemy import create_engine import pandas as pd engine create_engine( mysqlpymysql://10.200.13.59:9031/ads?charsetutf8, connect_args{ user: garciashan, # 注意这里要加 default_cluster: 前缀 password: garciashan250324, port: 9031 } ) df pd.read_sql(SELECT * FROM ads.ads_dim_site LIMIT 100, engine) # print(df) print(df.head()) print(df.shape)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2523461.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!