Pandas学习笔记 Series DataFrame

Series

import numpy as np
import pandas as pd
import sys
from pandas import Series,DataFrame

obj=Series([4,7,-5,3],index=['d','b','a','c'])
obj

obj[['d','c']]

obj['b']=6
obj
obj*2

obj[obj>2]

np.exp(obj)

sdata={'hi':35,'mi':'49','ji':59,'ki':89} #由字典创建序列
obj1=Series(sdata)
obj1

states=['hi','mi','ji','oi']
obj2=Series(sdata,index=states)
obj2

pd.isnull(obj2)  #等同obj2.isnull()

pd.notnull(obj2)

obj2+obj1

obj2.name='population'
obj2.index.name='satet'
obj2

DataFrame

data={'state':['China','China','China','Nevda','Nevda'],'year':[2000,2001,2002,2000,2001],'pop':[1.5,1.7,3.6,2.4,2.9]}#由字典创建数据框
frame=DataFrame(data) 
frame

frame1=DataFrame(data,columns=['year','state','pop'],index=['one','two','three','four','five'])#改变列的顺序以及添加行索引
frame1

frame1.year,frame1.columns

frame1.loc['three'] #第三行数据

frame1['debt']=16.5 #添加名为“debt”的一列,值都为16.5
frame1

frame1['debt']=np.arange(5.)
frame1

frame1['eastern']=frame1.state=='China' #若国家为中国,则为true
frame1

#由序列创建表格的列
val=Series([-1.2,-1.5,.7],index=['two','four','five'])
frame1['debt']=val
frame1

#产生以china,Nevda为列名,2000,2001为行索引的表格
pop={'China':{2000:2.4,2001:1.9},'Nevda':{2000:-1.5,2001:0.9}}
frame2=DataFrame(pop)
frame2

frame2.T

pdata={'China':frame2['China'][:],'Nevda':frame2['Nevda'][:]} #引用frame2的数据
frame3=DataFrame(pdata)
frame3

frame3.index.name='year'
frame3.columns.name='state'
frame3

frame3.values

obj3=Series(range(3),index=['a','b','c'])
obj3.index

index=pd.Index(np.arange(3))
obj4=Series([1.5,65,3],index=index)
obj4.index is index

'China'in frame3.columns

2003 in frame3.index

-1.5 in frame3.values

 

你可能感兴趣的:(Pandas,pandas,Series,DataFrame)