以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
导入 Packages
- from numpy import *
- from pandas import *
- #使用 import * 将 所有的包和类导入命名空间
- #对于巨大的包你可以通过 from [name] import [class/object]导入一部分
- #为了避免命名空间冲突,你也可以重命名你导入的部分
- #导入 pandas 并且重命名
- import pandas as pd
- #导入 Series 和 dataframe 类
- from pandas import Series, DataFrame
- #常见命名规则:numpy 是 np, pandas 是 pd ,matplotlib 是 plt
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
一、列表
1、生成列表
- # 列表 a
- a=[0,1,2,3,4,5,6,7,8,9]
- a
2、列表切片
- #使用 [x] to 访问在 list 的 x 的元素
- #所有 list 都从 0 开始索引
- print 'first item', a[0]
- #也可以通过使用 -1,-2 等等来索引最后一个、倒数第二个
- print 'last item', a[-1]
- #也可以使用 : 或 :: 进行切片处理
- print 'first three items', a[:3]
- print 'last three items', a[-3:]
- print 'start at the 4th item', a[3:]
- print 'the odd items', a[::2]
- #所有的 list 都有长度, 使用 len(list_name) 来获得list 中元素的数量
- #通常,如果 list 中包含数字,那么数学函数可以在 list 上应用
- print 'length of list', len(a)
- print 'largets number in list', max(a)
- print 'smallest number', min(a)
- print 'average', mean(a)
- #找到 max 和 min 的索引使用 argmax() 和 argmin()
- print 'the largest number in the list is', max(a),
- 'and is found at index:', argmax(a)
3、添加元素
- #通过使用 list_name.append(item),元素可以添加到 list 中
- #在列表 a 中添加元素 3
- a.append(3)
- #添加元素 4
- a.append(4)
- a
- #最后,通过使用 unique 方法,过滤 list 的重复数据
- #使用 sort(list_name)排序 list
- print 'sorted list', sort(a)
- print 'select distinct values', unique(a)
以上内容转自 数析学院,其他一些基础内容有时间再补齐,有需要的同学可以直接查看原文


雷达卡






京公网安备 11010802022788号







