1、numpy使用
数组中的排序,使用sort()方法
x = numpy.array([['8','9','5'],['1','2','3']])
x.sort()
print(x)
print(type(x))
运行结果:
[['5' '8' '9']
['1' '2' '3']]
<class 'numpy.ndarray'>
数组中取最大值和最小值,使用max()和min()方法
x = numpy.array([[8,9,5],[1,2,3]])
y = x.max()
z = x.min()
print(y)
print(z)
运行结果:
9
1
数组的运算效率比列表的运算效率要高
切片操作,按照下标取某一段元素
格式:
数组[起始下标:终止下标+1] # 跟列表相似
x = numpy.array([[8,9,5],[1,2,3]])
print(x[0][1:])
运行结果:
[9 5]
2、pandas用法
创建Serise数据
import pandas as pd
'''
Series 某一串数字,一行或者一列
DataFrame 数据框
'''
# 创建Serise数据
a = pd.Series([8,9,21])
print(a)
# 指定索引名
b = pd.Series([8,9,21], index=['one','two','three'])
print(b)
运行结果:
0 8
1 9
2 21
dtype: int64
one 8
two 9
three 21
dtype: int64
创建DataFrame数据
# 创建Dataframe数据
x = pd.DataFrame([[1,2,4],[8,7,4],[6,4,8]])
print(x)
运行结果:
0 1 2
0 1 2 4
1 8 7 4
2 6 4 8
为DataFrame数据指定列名
x = pd.DataFrame([[1,2,4],[8,7,4],[6,4,8]],columns=['one','two','three'])
print(x)
运行结果:
one two three
0 1 2 4
1 8 7 4
2 6 4 8
用字典创建数据框
x = pd.DataFrame({'one':4,'two':[6,2,3],'three':6})
print(x)
运行结果:
one two three
0 4 6 6
1 4 2 6
2 4 3 6
head()函数调取头部数据,默认显示前5行
tail()函数调取尾部数,默认显示后5行
shape()函数返回行和列
describe()统计数据的基本情况,默认按列统计
count 展示元素数量信息
mean 展示平均数
std 展示标准差信息
min 展示最小值
25% 每一列的分位数,前分位数
50%
75%
max 展示最大值
数据的转置,就是将行变成列,列变成行
x = pd.DataFrame({'one':4,'two':[6,2,3],'three':6})
print(x)
print(x.T)
运行结果:
one two three
0 4 6 6
1 4 2 6
2 4 3 6
0 1 2
one 4 4 4
two 6 2 3
three 6 6 6