一、Pandas的数据结构介绍
>>> from pandas import Series,DataFrame
>>> import pandas as pd
>>> import numpy as np
1.Series
Series:类似于一维数组的对象,由一组数据(各种numpy的数据类型)以及一组与之相关的数据标签(即索引)组成
>>> obj=Series([1,2,3,4])
#如果不指定索引,会自动生成从0-(N-1)的整数型索引
>>> obj
0 1
1 2
2 3
3 4
dtype: int64
>>> obj.values
array([1, 2, 3, 4])
>>> obj.index
RangeIndex(start=0, stop=4, step=1)
#numpy数组运算保留索引和值之间的关系
>>> obj[obj>2]
2 3
3 4
dtype: int64
>>> obj*2
0 2
1 4
2 6
3 8
dtype: int64
>>> np.exp(obj)
0 2.718282
1 7.389056
2 20.085537
3 54.598150
dtype: float64
#如果数据被存放在有一个python字典中,也可以直接通过这个字典创建Series
>>> score={"Tom":99,"Lucy":90,"John":80,"Green":58}
>>> score
{'John': 80, 'Green': 58, 'Lucy': 90, 'Tom': 99}
>>> obj_score=Series(score)
>>> obj_score
Green 58
John 80
Lucy 90
Tom 99
dtype: int64
#Series可以被看成是一个定长的有序字典,可以用很多原本需要字典参数的函数
>>> "Green" in obj_score
True
>>> "yaoxq" in obj_score
False
#将一个字典传入Series的索引,就可以得到匹配的值,“NaN”表示缺失或者NA值。
>>> name={"A","B","C","Tom"}
>>> obj_score_new=Series(obj_score,index=name)
>>> obj_score_new
A NaN
C NaN
B NaN
Tom 99.0
dtype: float64
#我们可以使用isnull和isnotnull来检测缺失数据
>>> pd.isnull(obj_score)
Green False
>>> from pandas import Series,DataFrame
>>> import pandas as pd
>>> import numpy as np
1.Series
Series:类似于一维数组的对象,由一组数据(各种numpy的数据类型)以及一组与之相关的数据标签(即索引)组成
>>> obj=Series([1,2,3,4])
#如果不指定索引,会自动生成从0-(N-1)的整数型索引
>>> obj
0 1
1 2
2 3
3 4
dtype: int64
>>> obj.values
array([1, 2, 3, 4])
>>> obj.index
RangeIndex(start=0, stop=4, step=1)
#numpy数组运算保留索引和值之间的关系
>>> obj[obj>2]
2 3
3 4
dtype: int64
>>> obj*2
0 2
1 4
2 6
3 8
dtype: int64
>>> np.exp(obj)
0 2.718282
1 7.389056
2 20.085537
3 54.598150
dtype: float64
#如果数据被存放在有一个python字典中,也可以直接通过这个字典创建Series
>>> score={"Tom":99,"Lucy":90,"John":80,"Green":58}
>>> score
{'John': 80, 'Green': 58, 'Lucy': 90, 'Tom': 99}
>>> obj_score=Series(score)
>>> obj_score
Green 58
John 80
Lucy 90
Tom 99
dtype: int64
#Series可以被看成是一个定长的有序字典,可以用很多原本需要字典参数的函数
>>> "Green" in obj_score
True
>>> "yaoxq" in obj_score
False
#将一个字典传入Series的索引,就可以得到匹配的值,“NaN”表示缺失或者NA值。
>>> name={"A","B","C","Tom"}
>>> obj_score_new=Series(obj_score,index=name)
>>> obj_score_new
A NaN
C NaN
B NaN
Tom 99.0
dtype: float64
#我们可以使用isnull和isnotnull来检测缺失数据
>>> pd.isnull(obj_score)
Green False