python数据分析学习笔记(一)

最近在看《利用Python进行数据分析·第2版》这本书,根据自己阅读进度,每次阅读后进行总结。可能涉及很多方面,但这不是一篇系统的教程。有兴趣的同学,可以直接去阅读这本书,网上已经有很多电子版,或者阅读其他教程。

问号使用

Jupyter notebook中,?有多种用法。shift + tab也可以显示相关帮助。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#变量问号
In []: b = [1, 2, 3]
In []: b?
Type: list
String Form:[1, 2, 3]
Length: 3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

#函数问号
In []: print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type: builtin_function_or_method

#函数源码
In []: add_numbers??
Signature: add_numbers(a, b)
Source:
def add_numbers(a, b):
"""
Add two numbers together

Returns
-------
the_sum : type of arguments
"""
return a + b
File: <ipython-input-9-6a548a216e27>
Type: function

魔术命令

%开头的命令,可以通过%magic%quickref查看所有命令,只在IPython中。下面列出常用的命令。

命令 说明
%hist 打印命令的输入(输出)历史
%run 运行代码
%time 语句单次执行时间
%timeit 语句多次运行平均时间
%xdel 删除变量并清空引用

格式输出

1
%[name][flags][width].[precision]typecode
  • name为命名。
  • flags可以有-,' '0。默认右对齐。-表示左对齐。' '为一个空格,表示在正数的左侧填充一个空格,从而与负数对齐。0表示使用0填充。
  • width表示显示宽度。
  • precision表示小数点后精度。
  • typecode为类型。
类型 说明
%s 字符串 (采用str()的显示)
%r 字符串 (采用repr()的显示)
%c 单个字符
%b 二进制整数
%d 十进制整数
%i 十进制整数
%o 八进制整数
%x 十六进制整数
%e 指数 (基底写为e)
%E 指数 (基底写为E)
%f or %F 浮点数
%g 指数(e)或浮点数 (根据显示长度)
%G 指数(E)或浮点数 (根据显示长度)
%% 字符%

序列函数

zip函数

zip可以将多个列表、元组或其它序列成对组合成一个元组列表,也可以解压一个序列:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#组合
In []: seq1 = ['foo', 'bar', 'baz']
In []: seq2 = ['one', 'two', 'three']

In []: list(zip(seq1, seq2))
Out[]: [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]

#解压
In []: pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'), ('Schilling', 'Curt')]
In []: first_names, last_names = zip(*pitchers)

In []: first_names
Out[]: ('Nolan', 'Roger', 'Schilling')

In []: last_names
Out[]: ('Ryan', 'Clemens', 'Curt')

sortsorted

1
2
3
4
5
In []: [7, 1, 2, 6, 0, 3, 2].sort()
Out[]: [0, 1, 2, 2, 3, 6, 7]

In []: sorted([7, 1, 2, 6, 0, 3, 2])
Out[]: [0, 1, 2, 2, 3, 6, 7]
-------------本文结束感谢您的阅读-------------
0%