python. pandas(series,dataframe,index) method test

python. pandas(series,dataframe,index,reindex,csv file read and write) method test

import pandas as pd
import numpy as np
def testpandas():
p = pd.Series([1,2,3,4,5],index =('a','b','c','d','e'))
print(p)

cities = {'bejing':5500,'shanghai':5999,'shezhen':6000,'suzhou':None}
p2 = pd.Series(cities,name ='prices')
print(p2[:-1])
print('bejing' in p2)
print(p2.get('bejing'))
print(p2[p2 < 6000])
print(p.mean())
s = pd.Series(np.random.randn(5),index =[1,2,3,4,5])
print(np.random.randn(5))

le = p2 < 5600
print(le)
print(p2[le])
print('---------------------------')
p2['bejing']=7000
print(p2/2)
print(np.log(p2))
print('---------------------------')
com=p + p2
print(com)
print('---------------------------')

data={'city':['bj','shenzhen','shanhai'],
'year':[2011,2013,2014],
'pop':[2100,2200,2430]}
df = pd.DataFrame(data,columns=['year','city','pop'],index=['one','two','three'])
print(df)
print('---------------------------')
df2=pd.DataFrame({'city':p2,'p1':p})
print(df2)
print('---------------------------')

data2=[{'july':9999,'han':5000,'zewei':1000},{'july':9999,'han':5000,'zewei':1000},{'july':9999,'han':5000,'zewe2i':1000}]
df3=pd.DataFrame(data2)
print(df3)
print(df3.loc[[1,2]])
print(df3['han'])
print('---------------------------')
print(df3.iloc[0:2])
print('---------------------------')
df3.loc[1]=9000
df3['han']=9000
print(df3)
print(df3.shape[1])
print(df3.columns)
print('---------------------------')
print(df3.info())
df3.index.name='city'
df3.columns.name='info'
print('---------------------------')
print(df3)
row =df3.loc[0]
print(row)

print(df3.sub(row,axis=1))
print('---------------------------')
col=df3['july']
print(col)
print(df3.sub(col,axis=0))
print('---------------------------')
index=pd.Index(['shanghai','guangzhou','shenzheng'])
print(index)
obj = pd.Series(range(3),index=['a','b','c'])
obj_index=obj.index
print(obj_index[1:])

print(df3.drop([0,1]))
print(df3)

print(df3)



#read and write csv of pandas
goog =pd.read_csv(r'C:pythondemoLiaoXueFengdata est_vrt.csv',index_col=0)
goog=goog.reindex(pd.to_datetime(goog.index))
print(goog.head())
print(goog.tail())
data2 = [{'july': 9999, 'han': 5000, 'zewei': 1000}, {'july': 9999, 'han': 5000, 'zewei': 1000},
{'july': 9999, 'han': 5000, 'zewe2i': 1000}]
df3 = pd.DataFrame(data2)
df3.to_csv(r'C:pythondemoLiaoXueFengdatagoog2.csv',encoding='GBK',mode='a')
原文地址:https://www.cnblogs.com/csj007523/p/7234893.html