Python数据处理
¶

01 Python介绍
¶

主讲人:丁平尖

我是谁¶

  • https://kdding.github.io/

为什么没有教材?¶

  • 网上有电子版
    • https://wesmckinney.com/book/
  • 大篇幅讲述函数和代码
    • 完全记不住
  • 书厚但不够丰富
  • 贵

数据科学彻底改变了这个世界¶

课程目标¶

  • 具有Python编程方面的背景

  • 介绍学术界/工业界流行的数据分析工具

  • 学习如何阅读文档并快速使用新工具

  • 学习基本的分布式计算框架、深度学习框架

These tools will be obsolete some day ... ... but not your ability to learn new frameworks and solve problems!

课程内容¶

  • Python介绍
    • Data types, functions, Jupyter, classes, objects
  • 数值计算与数据可视化
    • Numpy, scipy, matplotlib
  • 结构化数据处理
    • Regular expressions, retrieving web data, SQL, Python pandas, APIs
  • 大数据和并行编程
    • Basics of the UNIX command line, Hadoop, Spark, PyTorch

Python书籍推荐¶

  • Think Python
    • https://greenteapress.com/wp/think-python-2e/
  • Python for Everybody
    • https://www.py4e.com/book.php

学习第一步¶

  • 选择一个Python IDE或编辑器
    • 文本编辑器,Jupyter、Pycharm或VS Code
  • 熟悉jupyter
    • https://docs.jupyter.org/en/latest/start/index.html
  • 熟悉Pycharm
    • https://www.jetbrains.com/help/pycharm/jupyter-notebook-support.html
  • 熟悉VS Code
    • https://code.visualstudio.com/docs/datascience/jupyter-notebooks

本课程将全部使用Python3

Python简介¶

  • Python是一种动态类型、解释型编程语言

    • 由Guido van Rossum于1991年创建
    • 由Python软件基金会维护
    • 动态类型:在许多编程语言中,当你声明一个变量时,必须指定变量的类型(例如:int、double、Boolean,string)。而在Python中则不需要这样做。
    • 有些编程语言(如C/C++和Java)是编译型的:编写代码,然后通过编译生成一个可运行的程序。相比之下,Python是解释型的:一个称为解释器的程序逐行运行我们的代码。
  • 设计理念:简洁、可读的代码

  • Python语法与R、Java、C/C++、MATLAB不同

    • 使用空白符分隔
    • 较少使用括号、分号等符号

Python简介¶

for (i in 1:10) {
  print(i)
}
#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        std::cout << i << std::endl;
    }
    return 0;
}
for i = 1:10
    disp(i)
end
for i in range(1, 11):
    print(i)

Python简介¶

for (i in 1:10) {
  print(i)
}

R

#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        std::cout << i << std::endl;
    }
    return 0;
}

C++

for i = 1:10
    disp(i)
end

Matlab

for i in range(1, 11):
    print(i)

Python

使用空白符分隔;较少使用括号、分号等符号

运行Python¶

  • 如何运行Python

    • Python解释器

    • Jupyter:https://jupyter.org/

    • PythonAnywhere:https://www.pythonanywhere.com/

    • PyCharm:https://www.jetbrains.com/pycharm/

    • Visual Studio Code:https://code.visualstudio.com/

  • 作业以Jupyter Notebook的形式提交(.ipynb)

    • Jupyter安装:https://docs.jupyter.org/en/latest/install.html

    • 建议使用Anaconda安装Jupyter和管理环境:https://anaconda.com/

Python Interpreter on the Command Line¶

Python Interpreter on the Command Line¶

Python in Jupyter¶

  • 安装Jupyter
    • https://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html

Jupyter Notebook和Jupyter Lab¶

  • Jupyter Notebook是Jupyter项目的早期版本,提供了一个基于Web的交互式计算环境。

Jupyter Notebook和Jupyter Lab¶

  • Jupyter Lab是Jupyter项目的新一代交互式计算环境,提供了更加现代和灵活的用户界面。Jupyter Lab不仅包含了Console、terminal和其他工具集,可以在Jupyter Lab中同时打开多个工具进行操作。

启动Jupyter¶

Jupyter¶

第一个Python代码¶

PyCharm¶

  • https://www.jetbrains.com/help/pycharm/jupyter-notebook-support.html

PyCharm专业版才支持Jupyter Notebook

PyCharm¶

  • File > Settings > Project Notes > Python Interpreter

AI Assistant¶

  • AI Assistant
    • https://www.jetbrains.com/help/pycharm/ai-assistant.html
  • Github Copilot
    • https://github.com/features/copilot
  • Codeium
    • https://codeium.com/

AI Assistant¶

  • File > Settings > Plugins

Codeium¶

Version control¶

  • 版本控制
    • https://www.jetbrains.com/help/pycharm/version-control-integration.html
  • 不熟悉git的同学,可以到Bilibili上找找相关介绍视频

导出Html文件¶

  • 打开终端,激活相应环境
  • 安装nbconvert
    • pip install nbconvert
  • 将.ipynb文件导出为HTML
    • jupyter nbconvert --to html lecture01.ipynb

VS Code¶

  • https://code.visualstudio.com/docs

VS Code¶

  • 在VS Code中安装Python和Jupyter扩展

VS Code¶

  • 选择相应的解释器

VS Code¶

  • 选择相应的环境

VS Code¶

  • Terminal

VS Code¶

  • Version control and AI Assistant

Python中的算术¶

In [1]:
1 + 2 # 1 plus 2
Out[1]:
3
In [2]:
2 * 3 # 2 times 3
Out[2]:
6
In [3]:
2 ** 7  # 2 to the power of 7
Out[3]:
128
In [4]:
8 / 3 # 8 divided by 3
Out[4]:
2.6666666666666665
In [5]:
8 // 3 # 8 divided by 3 without remainder
Out[5]:
2
In [6]:
8 % 3 # 8 modulo 3
Out[6]:
2

数据类型¶

  • 程序处理不同值,将给予不同的类型;

    • 例如:
      • 值 42 是一个整数
      • 值2.71828 是一个浮点数(即小数)
      • 值 “bird” 是一个字符串(即一串字符)
  • 变量的类型决定了我们可以执行或不能执行的操作。

    • 例如,2 * 3 是合理的,但 ‘cat’*‘dog’ 是什么意思?

Variables in Python¶

In [7]:
# 与其它编程语言不同的是,Python自动指定变量类型
# 这种方式叫做 Duck Typing
mystring = "hello world!"
approxi_pi = 3.1415926
number_of_planet = 9
In [8]:
# 你可以使用 type() 函数来查看变量的类型
type(mystring)
Out[8]:
str
In [9]:
type(approxi_pi)
Out[9]:
float
In [10]:
type(number_of_planet)
Out[10]:
int

Variables in Python¶

  • Python变量类型的转化
In [11]:
approxi_pi = 3.1415926
type(approxi_pi)
Out[11]:
float
In [12]:
# convert a float to an integer
pi_int = int(approxi_pi)
type(pi_int)
Out[12]:
int
In [13]:
# convert an string to an integer
int_from_string = int('123456')
type(int_from_string)
Out[13]:
int
In [14]:
int_from_string
Out[14]:
123456
In [15]:
# 但是有些转换是没有意义的
goat_int = int('goat')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[15], line 2
      1 # 但是有些转换是没有意义的
----> 2 goat_int = int('goat')

ValueError: invalid literal for int() with base 10: 'goat'

一些字符串运算¶

In [16]:
# 一些运算也是没有意义的
'dog' * 'cat'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[16], line 2
      1 # 一些运算也是没有意义的
----> 2 'dog' * 'cat'

TypeError: can't multiply sequence by non-int of type 'str'
In [17]:
# concatenation of two strings
'dog' + 'cat'
Out[17]:
'dogcat'
In [18]:
# string concatenation with multiplication
'dog' * 3
Out[18]:
'dogdogdog'

Python中的注释¶

  • 两种注释'#'和''' '''
In [ ]:
# This is a comment.
# Python doesn't process this line.
euler = 2.71828
'''Triple quotes let you write a multiline like this one.
Everything between the quotes is part of the comment.
'''
print(euler)

作业¶

  • 安装并注册Anaconda、Jupyter、PyCharm(专业版)和VS Code

  • 安装并注册GitHub和Codeium

  • 阅读PyCharm或VS Code的技术文档