pandas入门(4)——基本功能

本文介绍了pandas的基本操作,包括重新索引、丢弃指定轴上的项、索引和过滤数据。重点讲解了loc和iloc的区别及用法,以及算术运算中的数据对齐和填充缺失值。此外,还讨论了DataFrame和Series之间的运算、排序与排名的方法。
摘要由CSDN通过智能技术生成

pandas入门(4)——基本功能

重新索引

方法reindex:其作用是创建一个新对象,它的数据符合新的索引,若某个索引值当前不存在,就引入缺失值

obj = pd.Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c'])
obj
obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])
obj2

方法method:使用ffill可以实现前项值填充,适用于时间序列等有序数据

obj3 = pd.Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])
obj3
obj3.reindex(range(6), method='ffill')

reindex借助DataFrame可以修改索引行和列

只传递一个序列,会重新索引结果的行

frame = pd.DataFrame(np.arange(9).reshape((3, 3)),
                     index=['a', 'c', 'd'],
                     columns=['Ohio', 'Texas', 'California'])
frame
frame2 = frame.reindex(['a', 'b', 'c', 'd'])
frame2

重新索引列使用columns关键字

states = ['Texas', 'Utah', 'California']
frame.reindex(columns=states)

丢弃指定轴上的项

drop方法

obj = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
obj
new_obj = obj.drop('c')
new_obj
obj.drop(['d', 'c'])

DataFrame中删除:

data = pd.DataFrame(np.arange(16).reshape((4, 4)),
                     index=['Ohio', 'Colorado', 'Utah', 'New York'],
                     columns=['one', 'two', 'three', 'four'])
data
#删除行
data.drop(['Colorado', 'Ohio'])
#删除列
data.drop('two', axis=1)
data.drop(['two', 'four'], axis='columns')

inplace方法

直接销毁所有被删除的数据,如

obj.drop('c', inplace=True)

索引、选取和过滤

Series数据结构

Series索引(obj[…])的工作方式类似于NumPy数组的索引,只

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值