pd.Dataframe基本操作之增删改查

#Dataframe基本操作之增删改查
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,index=['a1','a2','a3','a4'],columns=['r1','r2','r3','r4'])
print(df)
##Dataframe增加
#增加第五列r5
s1 = pd.Series(np.random.rand(4)*100,index=['a1','a2','a3','a4'])
df['r5'] = s1
print(df)
#增加第五,六行a5,a6
s2 = pd.Series(np.random.rand(5)*100,index=['r1','r2','r3','r4','r5'])
s3 = pd.Series(np.random.rand(5)*100,index=['r1','r2','r3','r4','r5'])
df.loc['a5'] = s2
df.loc['a6'] = s3
print(df)
##Dataframe删除
#del删第3列
      #del df['r3'](由于此方法会改变数据结构,索引此处注释掉)
#drop删第4,5行(直接删)
df1 = df.drop(['a5', 'a6'],axis=0)#axis默认是0,删除某行,垂直方向
print(df1)
#drop删第5列(直接删)
df2 = df.drop(['r5'], axis=1)#axis默认是0,删除某行,垂直方向
print(df2)
#drop删第5,6行(通过索引删,index:行索引)
df3 = df.drop(index=['a5','a6'],axis=0)
print(df3)
#drop删第5列(通过索引删,index:行索引)
df4 = df.drop(columns=['r5'],axis=1)
print(df4)
##Dataframe改
#利用loc函数直接对元素赋新值
df1.loc['a1','r4'] = 22
print(df1)
#列赋值
df2['r2'] = 0
print(df2)
#行赋值
val = pd.Series([1,2,3,4,5],index=['r1','r2','r3','r4','r5'])
df3.loc['a4'] = val  #如果存在a4则会修改,如果不存在a4则是增加一行a4
print(df3)
##Dataframe查 利用切片方法查询较简单(以下方法大多是切片)
q = pd.DataFrame(np.random.rand(3,4)*100,dtype=np.int,index=['one','two','three'],columns=list('abcd'))
print(q)
print(type(q.b))
print(q[['a','c']],type(q[['a','c']]))
print(q[1:2])#print(q.loc['two'])
print(q.loc['one':'two'])
print(q.iloc[0:2])#访问多行只能切片
print(q.iloc[1:,2:])#print(q[['c','d']].loc['two': 'three'])#print(q.loc['two': 'three',['c','d']])
print(q.loc['two': 'three'][['c','d']])


你可能感兴趣的:(pandas)