pandas基础知识(index)

jupyter notebook文件地址

import pandas as pd
import numpy as np
ser = pd.Series([5, 0, 3, 8, 4], index=['red', 'blue', 'yellow', 'yellow', 'blue'])
ser.index
Index(['red', 'blue', 'yellow', 'yellow', 'blue'], dtype='object')

重复标签

ser
red       5
blue      0
yellow    3
yellow    8
blue      4
dtype: int64
ser.idxmin()
‘blue’
ser.idxmax()
‘yellow’
ser.blue
blue    0
blue    4
dtype: int64
# 判断索引是否有重复
ser.index.is_unique
False

更换索引

ser = pd.Series([5, 0, 3, 8, 4], index=['one', 'two', 'three', 'four', 'five'])
ser
one 5 two 0 three 3 four 8 five 4 dtype: int64
# 产生一个新的对象
ser.reindex(['three', 'two', 'one', 'five', 'four'])
three 3 two 0 one 5 five 4 four 8 dtype: int64
# two变成six
ser.reindex(['three', 'six', 'one', 'five', 'four'])
three 3.0 six NaN one 5.0 five 4.0 four 8.0 dtype: float64
ser2 = pd.Series([1, 5, 6, 3], index=[0, 3, 5, 6])
ser2
0 1 3 5 5 6 6 3 dtype: int64
ser2.reindex(range(6), method='ffill')
0 1 1 1 2 1 3 5 4 5 5 6 dtype: int64
ser2.reindex(range(6), method='bfill')
0 1 1 5 2 5 3 5 4 6 5 6 dtype: int64
data = {
    'color':['blue', 'green', 'yellow', 'red', 'white'],
    'object':['ball', 'pen', 'pencil', 'paper', 'mug'],
    'price':[1.2, 1.0, 0.6, 0.9, 1.7]
}
frame = pd.DataFrame(data)
frame
colorobjectprice
0blueball1.2
1greenpen1.0
2yellowpencil0.6
3redpaper0.9
4whitemug1.7
frame.reindex(range(5, 0, -1),method='ffill', columns=['one', 'two', 'three'])
onetwothree
5mug1.71.7
4mug1.71.7
3paper0.90.9
2pencil0.60.6
1pen1.01.0
删除(drop)
frame
colorobjectprice
0blueball1.2
1greenpen1.0
2yellowpencil0.6
3redpaper0.9
4whitemug1.7
# 删除行
frame.drop([1])
colorobjectprice
0blueball1.2
2yellowpencil0.6
3redpaper0.9
4whitemug1.7
# 删除列 用关键字axis指定
frame.drop(['price'], axis=1)
colorobject
0blueball
1greenpen
2yellowpencil
3redpaper
4whitemug
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值