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']])


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值