python数据科学速查表
![](https://i-blog.csdnimg.cn/blog_migrate/265baa2c8fbf618704434a5b0dfa798f.png)
The printable version of this cheat sheet
该备忘单的可打印版本
The tough thing about learning data is remembering all the syntax. While at Dataquest we advocate getting used to consulting the Python documentation, sometimes it’s nice to have a handy reference, so we’ve put together this cheat sheet to help you out!
学习数据的困难之处在于记住所有语法。 在Dataquest时,我们提倡习惯于查阅Python文档 ,有时可以得到一些方便的参考,这很高兴,因此我们整理了这份备忘单,可以为您提供帮助!
This cheat sheet is the companion to our Python Basics Data Science Cheat Sheet
该备忘单是我们的Python基础数据科学备忘单的伴侣
If you’re interested in learning Python, we have a free Python Programming: Beginner course which can start you on your data science journey.
如果您对学习Python感兴趣,我们有一个免费的Python编程:入门课程,可以帮助您开始数据科学之旅。
Download a Printable PDF of this Cheat Sheet
关键基础知识,打印和获得帮助 (Key Basics, Printing and Getting Help)
This cheat sheet assumes you are familiar with the content of our Python Basics Cheat Sheet
该备忘单假定您熟悉我们的Python基础备忘单的内容
s s | A Python string variable | Python字符串变量 |
i i | A Python integer variable | Python整数变量 |
f f | A Python float variable | Python浮点变量 |
l l | A Python list variable | Python列表变量 |
d d | A Python dictionary variable | Python字典变数 |
清单 (Lists)
l.pop(3) l.pop(3) | l and deletes it from the listl 的第四项并将其从列表中删除 |
l.remove(x) l.remove(x) | l that is equal to l 中等于x x 的第一项 |
l.reverse() l.reverse() | l l 项目的顺序 |
l[1::2] l[1::2] | l , commencing from the l ,从开始1 st item1 日项目 |
l[-5:] l[-5:] | l l 中的最后5个项目 |
弦乐 (Strings)
s.lower() s.lower() | s s 的小写版本 | |
s.title() s.title() | s with the first letter of every word capitalizeds ,首字母大写 | |
"23".zfill(4) "23".zfill(4) | "0023" by left-filling the string with 0 左填充字符串以使其长度为0 ’s to make it’s length 4 来返回4 ."0023" 。 | |
s.splitlines() s.splitlines() | Returns a list by splitting the string on any newline characters. | 通过将字符串拆分为任何换行符来返回列表。 |
Python strings share some common methods with listsPython字符串与列表共享一些常用方法 | ||
s[:5] s[:5] | 5 characters of s 的前s 5 字符 | |
"fri" + "end" "fri" + "end" | "friend" "friend" | |
"end" in s "end" in s | True if the substring s 找到子字符串"end" is found in "end" 则返回s True |
范围 (Range)
Range objects are useful for creating sequences of integers for looping.
范围对象对于创建整数序列进行循环很有用。
range(5) range(5) | 0 to 0 到4 4 的序列 |
range(2000,2018) range(2000,2018) | 2000 to 2000 到2017 2017 的序列 |
range(0,11,2) range(0,11,2) | 0 to 0 到10 , with each item incrementing by 10 的序列,每一项增加2 2 |
range(0,-10,-1) range(0,-10,-1) | 0 to 0 到-9 -9 的序列 |
list(range(5)) list(range(5)) | 0 to 0 到4 4 的列表 |
辞典 (Dictionaries)
max(d, key=d.get) max(d, key=d.get) | d d 最大值的键 |
min(d, key=d.get) min(d, key=d.get) | d d 最小值的键 |
套装 (Sets)
my_set = set(l) my_set = set(l) | set object containing the l unique values from 唯一值的l set 对象 | |
len(my_set) len(my_set) | Returns the number of objects in my_set (or, the number of unique values from l ) | 返回my_set 中对象的my_set (或l 中唯一值的数量) |
a in my_set a in my_set | True if the value my_set 存在值a exists in a 则返回my_set True |
常用表达 (Regular expressions)
import re import re | Import the Regular Expressions module | 导入正则表达式模块 |
re.search("abc",s) re.search("abc",s) | match object if the regex s 找到正则表达式"abc" is found in "abc" ,则返回s , otherwise match 对象,否则返回None None | |
re.sub("abc","xyz",s) re.sub("abc","xyz",s) | "abc" are replaced by "abc" 匹配的实例都将替换为"xyz" "xyz" |
清单理解 (List comprehension)
A one-line expression of a for loop
for循环的单行表达式
[i ** 2 for i in range(10)] [i ** 2 for i in range(10)] | 0 to 0 到9 9 的值的平方的列表 |
[s.lower() for s in l_strings] [s.lower() for s in l_strings] | l_strings , with each item having had the l_strings ,每一项都应用.lower() method applied.lower() 方法 |
[i for i in l_floats if i < 0.5] [i for i in l_floats if i < 0.5] | l_floats that are less than l_floats 返回小于0.5 0.5 |
循环功能 (Functions for looping)
| l , printing the index location of each item and its valuel ,打印每个项目的索引位置及其值 |
l_one and l_one 和l_two and print each valuel_two 并打印每个值 | |
| x is no longer less than x 的值不再小于10 10 |
约会时间 (Datetime)
import datetime as dt import datetime as dt | datetime moduledatetime 模块 |
now = dt.datetime.now() now = dt.datetime.now() | datetime object representing the current time to datetime 对象到now now |
wks4 = dt.datetime.timedelta(weeks=4) wks4 = dt.datetime.timedelta(weeks=4) | timedelta object representing a timespan of 4 weeks to timedelta 对象分配给wks4 wks4 |
now - wks4 now - wks4 | datetime object representing the time 4 weeks prior to datetime 对象表示时间前4周now now |
newyear_2020 = dt.datetime(year=2020, month=12, day=31) newyear_2020 = dt.datetime(year=2020, month=12, day=31) | datetime object representing December 25, 2020 to datetime 对象分配给newyear_2020 newyear_2020 |
newyear_2020.strftime("%A, %b %d, %Y") newyear_2020.strftime("%A, %b %d, %Y") | "Thursday, Dec 31, 2020" "Thursday, Dec 31, 2020" |
dt.datetime.strptime('Dec 31, 2020',"%b %d, %Y") dt.datetime.strptime('Dec 31, 2020',"%b %d, %Y") | datetime object representing December 31, 2020datetime 对象 |
随机 (Random)
import random import random | random modulerandom 模块 |
random.random() random.random() | 0.0 and 0.0 和1.0 1.0 之间的随机浮点数 |
random.randint(0,10) random.randint(0,10) | 0 and 0 到10 10 之间的随机整数 |
random.choice(l) random.choice(l) | l l |
计数器 (Counter)
from collections import Counter from collections import Counter | Counter classCounter 类 |
c = Counter(l) c = Counter(l) | Counter (dict-like) object with the counts of each unique item from Counter (类似dict的对象),其对象中每个唯一项的计数从l , to l 到c c |
c.most_common(3) c.most_common(3) | l l 最常见的3个项目 |
尝试/除外 (Try/Except)
Catch and deal with errors
捕捉并处理错误
| l_ints l_ints |
l_ints to a float, catching and handling l_ints 每个值转换为浮点数,捕获并处理ValueError: could not convert string to float: where values are missing.ValueError: could not convert string to float: 缺少值的地方。 |
下载此备忘单的可打印版本 (Download a printable version of this cheat sheet)
翻译自: https://www.pybloggers.com/2017/08/python-cheat-sheet-for-data-science-intermediate/
python数据科学速查表