Pandas库入门

Pandas库入门

pandas有两大数据类型seriesdataframe

Series

import pandas as pd
pd.Series(range(20))
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    10
11    11
12    12
13    13
14    14
15    15
16    16
17    17
18    18
19    19
dtype: int64

这个就是Series,左边的是index,右边的是value

Series的创建方法

由列表创建

pd.Series([9, 8, 7, 6])
0    9
1    8
2    7
3    6
dtype: int64

其中参数index就是索引,默认是自定义索引,即从0开始

pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
a    9
b    8
c    7
d    6
dtype: int64

由标量创建

pd.Series(25, ['a', 'b', 'c'])
a    25
b    25
c    25
dtype: int64

由字典创建

pd.Series({'a':9, 'b':8, 'c':7})
a    9
b    8
c    7
dtype: int64
pd.Series({'a':9, 'b':8, 'c':7}, index=['c', 'a', 'b', 'd'])
c    7.0
a    9.0
b    8.0
d    NaN
dtype: float64

由ndarray类型创建

import numpy as np
pd.Series(np.arange(5))
0    0
1    1
2    2
3    3
4    4
dtype: int32
pd.Series(np.arange(5), index=np.arange(9, 4, -1))
9    0
8    1
7    2
6    3
5    4
dtype: int32
Series的属性
b = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
b
a    9
b    8
c    7
d    6
dtype: int64
b.index
Index(['a', 'b', 'c', 'd'], dtype='object')
b.values
array([9, 8, 7, 6], dtype=int64)

通过索引获得值

b['b']

8

但是Series含有默认索引即从0开始的下标

b[1]

8

b[3]

6

b[:3]
a    9
b    8
c    7
dtype: int64
b[b > b.median()] 
a    9
b    8
dtype: int64

可以看出对Series进行切片等操作返回值仍然为Series类型

'c' in b # 判断索引是否存在
True
0 in b
False
b.get('f', 100) # 获得f索引所对应的值,若b中不存在f索引,则返回100
100

Series之间的运算

a = pd.Series([1, 2, 3], ['c', 'd', 'e'])
b = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
a + b # Series相加是索引相同的值相加
a    NaN
b    NaN
c    8.0
d    8.0
e    NaN
dtype: float64

DataFrame

DateFrame 横轴:column axis=1 纵轴:index axis=0

pd.DataFrame(np.arange(10).reshape(2, 5))
01234
001234
156789

这个就是一个DataFrame

创建DataFrame的方法

通过字典创建

dt = {'one': pd.Series([1, 2, 3], ['a', 'b', 'c']),
     'two': pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])}
d = pd.DataFrame(dt)
d
onetwo
a1.09
b2.08
c3.07
dNaN6
pd.DataFrame(dt, index=['b', 'c', 'd'], columns=['two', 'three']) 
twothree
b8NaN
c7NaN
d6NaN
dl = {'城市': ['北京', '上海', '广州', '深圳', '沈阳'],
     '环比': [101.5, 101.2, 101.3, 102.0, 100.1],
     '同比': [120.7, 127.3, 119.4, 140.9, 101.4],
     '定基': [121.4, 127.8, 120.0, 145.5, 101.6]}
d = pd.DataFrame(dl, index=['c1', 'c2', 'c3', 'c4', 'c5'])
d
城市环比同比定基
c1北京101.5120.7121.4
c2上海101.2127.3127.8
c3广州101.3119.4120.0
c4深圳102.0140.9145.5
c5沈阳100.1101.4101.6
d.index
Index(['c1', 'c2', 'c3', 'c4', 'c5'], dtype='object')
d.columns
Index(['城市', '环比', '同比', '定基'], dtype='object')
d.values
array([['北京', 101.5, 120.7, 121.4],
       ['上海', 101.2, 127.3, 127.8],
       ['广州', 101.3, 119.4, 120.0],
       ['深圳', 102.0, 140.9, 145.5],
       ['沈阳', 100.1, 101.4, 101.6]], dtype=object)
d['同比']
c1    120.7
c2    127.3
c3    119.4
c4    140.9
c5    101.4
Name: 同比, dtype: float64
d['同比']['c2']
127.3
d = d.reindex(index=['c5', 'c4', 'c3', 'c2', 'c1'])
d = d.reindex(columns=['城市', '同比', '环比', '定基'])
d
城市同比环比定基
c5沈阳101.4100.1101.6
c4深圳140.9102.0145.5
c3广州119.4101.3120.0
c2上海127.3101.2127.8
c1北京120.7101.5121.4

reindex函数改变索引顺序

drop函数删除指定的行或者列

d.drop('c5')
城市同比环比定基
c4深圳140.9102.0145.5
c3广州119.4101.3120.0
c2上海127.3101.2127.8
c1北京120.7101.5121.4
d.drop('同比', axis=1) # drop默认操作0轴上的元素
城市环比定基
c5沈阳100.1101.6
c4深圳102.0145.5
c3广州101.3120.0
c2上海101.2127.8
c1北京101.5121.4
DataFrame之间的运算
a = pd.DataFrame(np.arange(12).reshape(3, 4))
b = pd.DataFrame(np.arange(20).reshape(4, 5))
a + b
01234
00.02.04.06.0NaN
19.011.013.015.0NaN
218.020.022.024.0NaN
3NaNNaNNaNNaNNaN

实际上就是索引相同的值之间进行运算

b.add(a, fill_value=100) # 先用100填充a
01234
00.02.04.06.0104.0
19.011.013.015.0109.0
218.020.022.024.0114.0
3115.0116.0117.0118.0119.0
c = pd.Series(np.arange(4))
c - 10
0   -10
1    -9
2    -8
3    -7
dtype: int32
b - c # 横向运算
01234
00.00.00.00.0NaN
15.05.05.05.0NaN
210.010.010.010.0NaN
315.015.015.015.0NaN
b.sub(c, axis=0)
01234
001234
145678
289101112
31213141516

布尔运算

a = pd.DataFrame(np.arange(12).reshape(3, 4))
b = pd.DataFrame(np.arange(12, 0, -1).reshape(3, 4))
a > b
0123
0FalseFalseFalseFalse
1FalseFalseFalseTrue
2TrueTrueTrueTrue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值