1.pandas的Series对象
pandas的Series对象是一个带索引数据构成的一维数组。可以用一个数组创建Series对象
import pandas as pd
data=pd.Series([0.25,0.5,0.75,1.0])
print(data)
# output:0 0.25
# 1 0.50
# 2 0.75
# 3 1.00
# dtype: float64
在上面的运行结果中,Series对象将一组数据和一组索引绑定在一起,我们可以通过values属性和index属性获取数据。
data.values
#output;[0.25 0.5 0.75 1. ]
data.index
#output:RangeIndex(start=0, stop=4, step=1)
另外,数据可以通过python的中括号索引标签获取:
data[1]
#output:0.5
data[:3]
#output:1 0.50
# 2 0.75
# dtype: float64
1.1 Series是通用的Numpy数组
从上面的例子看出来,Numpy的一维数组和Series对象基本可以等价交换,但是两者的本质差异是存在于索引:Numpy数组是通过隐式定义的整数索引获取数值,而pandas的Series对象是用一种显式定义的索引与数值关联。
显式索引的定义让Series对象拥有了更强的定义。例如:索引不再仅仅是整数,还可以是任意想要的类型。
data=pd.Series([0.25,0.5,0.75,1.0],index=['a','b','c','d'])
print(data)
print(data['a'])
#output:a 0.25
# b 0.50
# c 0.75
# d 1.00
# dtype: float64
# 0.25
注意:也可以使用不连续或不按顺序的索引。
data=pd.Series([0.25,0.5,0.75,1.0],index=[2,5,3,7])
1.2 Series是特殊的字典
Series对象看成是一种特殊的Python字典。字典是一种将任意键映射到一组任意值的数据结构,而Series对象其实是一组类型键映射到一组类型值的数据结构。
import pandas as pd
population_dict={'California':38332521,'Texas':26448193,'New York':19651127,'Florida':19552860,'Illinois':12882135}
population=pd.Series(population_dict)
print(population)
#output:California 38332521
# Texas 26448193
# New York 19651127
# Florida 19552860
# Illinois 12882135
# dtype: int64
上面对数组的操作仍然可以对这个Series对象适用。如切片操作,取值等。
1.3 创建Series对象
一般的创建方式:pd.Series(data,,index=index)
a)data可以是列表或者Numpy,这时index默认值为整数序列
z=pd.Series([2,4,6])
print(z)
#output:0 2
# 1 4
# 2 6
# dtype: int64
b)data也可以是一个标量,创建Series对象时会重复填充到每个索引上:
z=pd.Series(5,index=[100,200,300])
print(z)
#output:100 5
# 200 5
# 300 5
# dtype: int64
c)data还可以是一个字典,index默认是排序的字典键:
z=pd.Series({2:'a',1:'b',3:'c'},index=[3,2])
print(z)
#output:3 c
# 2 a
# dtype: object
#出现这样的原因是:Series对象只会保留显式定义的键值对。
2.pandas的DataFrame对象
2.1 DataFrame是通用的Numpy数组
可以将DataFrame看作是一种既有灵活的行索引,又有灵活的列名的二维数组。它的行和列都可以通过索引获取
同时,可以将DataFrame看成是有序‘排列’的若干Series对象
#创建基本的DataFrame,采用Series对象来解决。
import pandas as pd
population_dict={'California':38332521,'Texas':26448193,'New York':19651127,'Florida':19552860,'Illinois':12882135}
population=pd.Series(population_dict)
area_dict={'California':423967,'Texas':170312,'New York':141297,'Florida':170312,'Illinois':149995}
area=pd.Series(area_dict)
#用一个字典创建一个包含这些信息的二维数组:
states=pd.DataFrame({'population':population,'area':area})
print(states)
#output: population area
# California 38332521 423967
# Texas 26448193 170312
# New York 19651127 141297
# Florida 19552860 170312
# Illinois 12882135 149995
#查看行索引标签(index 属性)
states.index
#output:Index(['California', 'Texas', 'New York', 'Florida', 'Illinois'], dtype='object')
#返回的是Index对象
#查看列索引标签
states.columns
#output:Index(['population', 'area'], dtype='object')
2.2 DataFrame是特殊的字典
我们可以把DataFrame看成是特殊的字典。字典是一个键映射一个值,而DataFrame是一列映射一个Series的数据。
# 通过‘area’列属性获取包含于area列的所有数据
print(states['area'])
#output:California 423967
# Texas 170312
# New York 141297
# Florida 170312
# Illinois 149995
# Name: area, dtype: int64
2.3 创建DataFrame对象
a)通过单个Series对象创建
#DataFrame 是一组Series对象的集合,可以使用单个Series创建一个单列的DataFrame
pd.DataFrame(population,columns=['populations'])
#output populations
# California 38332521
# Texas 26448193
# New York 19651127
# Florida 19552860
# Illinois 12882135
b)通过字典列表创建
任何元素是字典的列表都可以变成DataFrame.用一个简单的列表综合来创建一些数据:
data=[{'a':i,'b':2*i}for i in range(3)]
z=pd.DataFrame(data,index=list('ABC'))
print(z)
#output: a b
# A 0 0
# B 1 2
# C 2 4
当字典中有些键不存在时,Pandas也会使用缺失值(NaN)(not a number)来表示:
data=[{'a':1,'b':2},{'b':3,'c':4}]
z=pd.DataFrame(data)
print(z)
#output: a b c
# 0 1.0 2 NaN
# 1 NaN 3 4.0
c)通过Series对象字典创建
见最开始的示例中DataFrame的创建方式。
d)通过Numpy二维数组创建
假如有一个二维数组,就可以创建一个可以指定行列索引值的DataFrame.如果不指定行列索引值,那么行列默认都是整数索引值:
data=np.random.rand(3,2)
z=pd.DataFrame(data,columns=['foo','bar'],index=['a','b','c'])
print(z)
#output: foo bar
# a 0.679849 0.791610
# b 0.438278 0.331297
# c 0.998745 0.861642
e)通过Numpy结构化数组创建
首先是介绍来自于官网的结构化数组:
Structured type, two fields: the first field contains an unsigned int, the second an int32:
>> np.dtype([('f1', np.uint), ('f2', np.int32)])
#output:dtype([('f1', '<u4'), ('f2', '<i4')])
通过结构化数组创建DataFrame:
A=np.zeros(3,dtype=[('A','i8'),('b','f8')])
print(A)
#output:[(0, 0.) (0, 0.) (0, 0.)]
z=pd.DataFrame(A)
print(z)
#output: A B
# 0 0 0.0
# 1 0 0.0
# 2 0 0.0
3.pandas的Index对象
先简单的创建Index对象数组
import pandas as pd
index=pd.Index([2,3,5,7,11])
print(index)
#output:Int64Index([2, 3, 5, 7, 11], dtype='int64')
3.1 将Index看做不可变数组
Index对象的许多操作都类似于数组,可以采用Python标准的取值方法获取数值,也可以通过切片获取数值:
index[1]
#output:3
#通过切片获取数值
# 每隔一个值取一个,一下子看蒙了
print(index[::2])
但是,Index对象 里面的值是不可更改的,如果这样:index[0]=1会报错,这就是不可修改的含义。
3.2 将Index看做有序集合
Index对象遵循Python标准库的集合(set)数据结构的许多习惯用法,包括并集、交集、差集等。
indA=pd.Index([1,3,5,7,9])
indB=pd.Index([2,3,5,7,11])
# 交集
print(indA&indB)
# 并集
print(indA|indB)
# 异或
print(indA^indB)
#output:Int64Index([3, 5, 7], dtype='int64')
# Int64Index([1, 2, 3, 5, 7, 9, 11], dtype='int64')
# Int64Index([1, 2, 9, 11], dtype='int64')
https://blog.csdn.net/sir_TI/article/details/83478146