首页 > 代码库 > Python pandas
Python pandas
from pandas import Series,DataFrame
import pandas as pd
‘‘‘index
obj2= Series([4,7,-5,3],index=[‘d‘,‘b‘,‘a‘,‘c‘])
obj2.values
obj2.index
obj2[‘a‘]
obj2[‘c‘]
obj2[‘d‘] = 6
obj2[[‘c‘,‘d‘,‘a‘]]
obj2
obj2[obj2>0]
obj2*2
import numpy as np
np.exp(obj2)
‘‘‘ dict
‘b‘ in obj2
‘e‘ in obj2
data = http://www.mamicode.com/{‘Ohio‘:35000,‘Texas‘:71000,‘Oregon‘:16000,‘Utah‘:5000}
obj3 = Series(data)
obj3
‘‘‘passing a dict
states = [‘California‘,‘Ohio‘,‘Oregon‘,‘Texas‘]
obj4 = Series(data,index=states)
‘‘‘missing data
pd.isnull(obj4)
pd.notnull(obj4)
‘‘‘auto align index
obj3
obj4
obj3+obj4
‘‘‘name and rename
obj4.name = ‘population‘
obj4.index.name = ‘state‘
obj4
obj2.index=[‘Bob‘,‘Helen‘,‘Jeff‘,‘Ryan‘]
‘‘‘dataframe,by default by name order
data=http://www.mamicode.com/{‘state‘:[‘Ohio‘,‘Ohio‘,‘Ohio‘,‘Nevada‘,‘Nevada‘],
‘year‘:[2000,2001,2002,2001,2002],
‘pop‘:[1.5,1.7,3.6,2.4,2.9]}
frame = DataFrame(data)
‘‘‘order by special order
DataFrame(data,columns =[‘year‘,‘state‘,‘pop‘])
‘‘‘ add one column and index
frame2= DataFrame(data,columns=[‘year‘,‘state‘,‘pop‘,‘debt‘],index=[‘one‘,‘two‘,‘three‘,‘four‘,‘five‘])
frame2.columns
‘‘‘get value by columns
frame2[‘state‘]
‘‘‘get value by index
frame2.ix[‘three‘]
‘‘‘assignment by index
frame2[‘debt‘] = 16.5
frame2[‘debt‘] = np.arange(5.)
val = Series([-1.2,-1.5,-1.7],index=[‘two‘,‘four‘,‘five‘])
frame2[‘debt‘] = val
‘‘‘del columns
frame2[‘eastern‘] = frame2.state == ‘Ohio‘
del frame2[‘eastern‘]
‘‘‘nest dict
pop={‘Nevada‘:{2001:2.4,2002:2.9},‘Ohio‘:{2000:1.5,2001:1.7,2002:3.6}}
frame3=DataFrame(pop)
‘‘‘transpose the frame
frame3.T
‘‘‘index changes different with series
DataFrame(pop,index=[2001,2002,2003])
‘‘‘dataframe get value
pdata = http://www.mamicode.com/{‘Ohio‘:frame3[‘Ohio‘][:-1],
‘Nevada‘:frame3[‘Nevada‘][:2]}
DataFrame(pdata)
‘‘‘data frame index and column name attribute
frame3.index.name = ‘year‘;
frame3.columns.name = ‘state‘
frame3
Python pandas
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。