Pandas数据结构之Series

import pandas as pd
#Series类
#生成series类的方法:1.
obj = pd.Series([4, 7, -5, 3])
obj2 = pd.Series([4, 7, 5, -3], index=['a', 'b', 'c', 'd'])
print(obj2.values, obj2.index)
print(obj2['a'])
print(obj2[['c', 'a', 'd']]) #必须要加两层中括号
print(obj[obj > 0])
print(obj2[obj2 > 0])


#2.直接使用字典生成series
sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
obj3 = pd.Series(sdata)
print(obj3)
states = ['California', 'Ohio', 'Oregon', 'Texas']
obj4 = pd.Series(sdata, index=states)
print(obj4)
#NAN标记缺失值的方式
print(obj4.isnull())
print(obj4.notnull())
print(obj3+obj4)#相当于数据库中的join操作

#给series对象及其索引进行命名
obj4.name = 'population'
obj4.index.name = 'state'
print(obj4)
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
print(obj)
[ 4  7  5 -3] Index(['a', 'b', 'c', 'd'], dtype='object')
4
c    5
a    4
d   -3
dtype: int64
0    4
1    7
3    3
dtype: int64
a    4
b    7
c    5
dtype: int64
Ohio      35000
Oregon    16000
Texas     71000
Utah       5000
dtype: int64
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64
California     True
Ohio          False
Oregon        False
Texas         False
dtype: bool
California    False
Ohio           True
Oregon         True
Texas          True
dtype: bool
California         NaN
Ohio           70000.0
Oregon         32000.0
Texas         142000.0
Utah               NaN
dtype: float64
state
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
Name: population, dtype: float64
Bob      4
Steve    7
Jeff    -5
Ryan     3
dtype: int64

 

 

展开阅读全文

没有更多推荐了,返回首页