Python pandas入门
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
a = Series([1,2,3.0,'abc'])
print(a)
b = Series(data = [1,2,3,4],index = ['a','b','c','d'])
print(b)
print(b.values)
print(b.index)
c = Series({
'a':1,'b':2})
print(c)
c.name = 'cSeries'
c.index.name = 'cIndex'
print(c)
data = {
'state':['Ohino','Ohino','Ohino','Nevada','Nevada'],'year':[2000,2001,2002,2001,2002],'pop':[1.5,1.7,3.6,2.4,2.9]}
df = DataFrame(data)
print(df)
df = DataFrame(data,index=['one','two','three','four','five'],columns=['year','state','pop','debt'])
print(df)
print(df.index)
print(df.columns)
print(type(df['pop']))
print(df['pop'])
print(b)
e = b.reindex(['b','d','a','c','e'],fill_value=0)
print(e)
e = b.reindex(['b','d','a','c','e'],method='ffill')
print(e)
f = DataFrame(np.arange(9).reshape((3,3)),index = ['a','b','c'],columns=['c1','c2','c3'])
print(f)
f1 = f.reindex(columns=['c2','c3','c1'])
print(f1)
f3 = f.reindex(index=['c','b','a'])
print(f3)
f2 = f.reindex(index = ['c','d','a','b'],columns=['c2','c3','c1'],fill_value=0)
print(f2)
f4 = f.drop('c')
print(f4)
f5 = f.drop(['c1','c2'],1)
print(f5)
f5 = f.drop('c1',1)
print(f5)
b1 = b.drop(['a','c'])
print(b1)
g= DataFrame(np.arange(16).reshape((4,4)),index = ['a','b','c','d'],columns=['c1','c2','c3','c4'])
print(g)
g1 = g.loc[:,:'c2']
print(g1)
g2 = g.iloc[:,:2]
print(g2)
print(g.iloc[1,1])
print(g.loc['b','c2'])
print(g['c1'])
print(g['c2']>5)
g3 = g[g['c2']>5]
print(g3)
print(g[:'c'])
print(g.loc['c',:])
print(g.loc[:,'c1'])
h1 = DataFrame(np.arange(16).reshape((4,4)),index = ['a','b','c','d'],columns=['c1','c2','c3','c4'])
h2 = DataFrame(np.arange(16).reshape((4,4)),index = ['b','c','d','e'],columns=['c1','c2','c3','c4'])
print(h1+h2)
h3 = h1.add(h2,fill_value=0)
print(h3)
print(h1.mean())
print(h1.min())
h1.drop(["c2"],axis=1)
print(h1)
h1.drop(["c2"],axis=1,inplace=True)
print(h1)