Skip to content

Python | Pandas | Pandas Objects

Posted on:February 18, 2019

NumPy擅长:

NumPy不擅长:

Pandas,建立在NumPy数组结构上,十分擅长数据清理(data munging)。标准导入:

import numpy as np
import pandas as pd

Table of contents

1. Series对象

1.1 简介

简单来看,Series就是带有“索引数据”的“一维数组”(类似于字典):

# 可以直接传入列表来创建Seires对象
# 默认索引(隐式索引)从0开始,到len-1
pd.Series([13,42,89,57])
0    13
1    42
2    89
3    57
dtype: int64

Series对象有两个重要的属性:

data = pd.Series([13,42,89,57])
print(type(data.values))
print(data.values)
print(type(data.index))
print(data.index)
<class 'numpy.ndarray'>
[13 42 89 57]
<class 'pandas.core.indexes.range.RangeIndex'>
RangeIndex(start=0, stop=4, step=1)

Series的索引不仅可是整数,还可是任意想要的类型——在创建Series时指定index为特定的参数即可:

# 显式索引
pd.Series([0.24,1.32,4.31,8.64], index=['a','b','c','d'])
a    0.24
b    1.32
c    4.31
d    8.64
dtype: float64

可以从Series与数组、字典的对比中理解其特点:

1.2 创建

创建Series的方法:

# 传入一个标量
pd.Series(5, index=['a','b','c','d'])
a    5
b    5
c    5
d    5
dtype: int64
# 传入一个字典
pd.Series({'a':1,'b':2,'c':3,'d':4})
a    1
b    2
c    3
d    4
dtype: int64
# 根据主动传入的index进行筛选
pd.Series({'a':1,'b':2,'c':3,'d':4}, index=['b','d','f'])
b    2.0
d    4.0
f    NaN
dtype: float64

2. DataFrame对象

2.1 简介

第一,DataFrame可被看做有序排列的若干具有相同索引的Series——下例中的df由两个Series构成:

weight = pd.Series([64,73,66,81], index=['Sam','Bill','Lucy','Eric'])
height = pd.Series([178,182,175,187], index=['Sam','Bill','Lucy','Eric'])
df = pd.DataFrame({'weight':weight, 'height':height})
df
weight height
Sam 64 178
Bill 73 182
Lucy 66 175
Eric 81 187

第二,DataFrame可以看做既有行索引(index)也有列索引(columns)的二维数组:

df.index
Index(['Sam', 'Bill', 'Lucy', 'Eric'], dtype='object')
df.columns
Index(['weight', 'height'], dtype='object')

第三,DataFrame还可以看做字典,字典的key是“列名”,字典的value是“那一列的Series”:

df['weight']
Sam     64
Bill    73
Lucy    66
Eric    81
Name: weight, dtype: int64
df['height']
Sam     178
Bill    182
Lucy    175
Eric    187
Name: height, dtype: int64

一些操作:

# 转换为二维数组
df.values
array([[ 64, 178],
       [ 73, 182],
       [ 66, 175],
       [ 81, 187]])
# 转置
df.T
Sam Bill Lucy Eric
weight 64 73 66 81
height 178 182 175 187

2.2 创建

①字典形式: 每一列

# 传入一个字典,每个键值对的key是列名,value是Series
weight = pd.Series([64,73,66,81], index=['Sam','Bill','Lucy','Eric'])
height = pd.Series([178,182,175,187], index=['Sam','Bill','Lucy','Eric'])
df = pd.DataFrame({'weight':weight, 'height':height})
df
weight height
Sam 64 178
Bill 73 182
Lucy 66 175
Eric 81 187

②列表形式: 每一行

# 一个字典是一行数据
df = pd.DataFrame([{'a':1, 'b':2}, {'b':2, 'c':3}])
df
a b c
0 1.0 2 NaN
1 NaN 2 3.0

③二维数组形式: 整个矩阵

pd.DataFrame(np.random.randint(0,99,(3,4)), index=[1,2,3], columns=['one','two', 'three', 'four'])
one two three four
1 47 86 83 92
2 77 24 61 28
3 63 88 79 46

index或columns若不显式指定,都从0开始标注(隐式索引);可以使用index和columns参数指定。

2.3 索引筛选

weight = pd.Series([64,73,66,81], index=['Sam','Bill','Lucy','Eric'])
height = pd.Series([178,182,175,187], index=['Sam','Bill','Lucy','Eric'])
df = pd.DataFrame({'weight':weight, 'height':height}, columns=['weight','age'])
df
weight age
Sam 64 NaN
Bill 73 NaN
Lucy 66 NaN
Eric 81 NaN

3. Index对象