Python数据分析——相关模块的基本使用

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

General_单刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值