好用却可能不为人熟知的 Pandas 小众函数 1

看到了几个不常用的Pandas函数,虽然暂时用不到,但先记下来 以后总有可能用到
总目录

为了举例,调取 Pandas 数据。
由于前段时间做股票期权数据,那个数据类型比较全,调用方便,所以这里使用它。

  • 使用 Tushare,随便调用一个数据。
import pandas as pd
import tushare as tus
pro = tus.pro_api()
df = pro.opt_basic(exchange='SSE')
df.head(3)
ts_codeexchangenameper_unitopt_codeopt_typecall_putexercise_typeexercise_prices_monthmaturity_datelist_pricelist_datedelist_datelast_edatelast_ddatequote_unitmin_price_chg
010000579.SHSSE华夏上证50ETF期权1604认购2.1510000.0OP510050.SHETF期权C欧式2.15201604201604270.041220160225201604272016042720160428人民币元0.0001
110000108.SHSSE华夏上证50ETF期权1505认购2.6510000.0OP510050.SHETF期权C欧式2.65201505201505270.100620150326201505272015052720150528人民币元0.0001
210000111.SHSSE华夏上证50ETF期权1505认沽2.5510000.0OP510050.SHETF期权P欧式2.55201505201505270.090620150326201505272015052720150528人民币元0.0001

cut() 数值划分成等额的数份

  • cut() 能将数值划分成等额的数份
# 期权价格的数据类型为 float
df['exercise_price'].dtype
dtype('float64')
pd.cut(df['exercise_price'],5)
0       (1.797, 2.36]
1        (2.36, 2.92]
2        (2.36, 2.92]
3        (2.92, 3.48]
4        (2.92, 3.48]
            ...      
2517     (2.92, 3.48]
2518     (2.92, 3.48]
2519     (2.92, 3.48]
2520     (2.92, 3.48]
2521     (2.92, 3.48]
Name: exercise_price, Length: 2522, dtype: category
Categories (5, interval[float64]): [(1.797, 2.36] < (2.36, 2.92] < (2.92, 3.48] < (3.48, 4.04] < (4.04, 4.6]]
  • 如上面的输出,该函数 按大约相差 0.56 把 期权价分成了五份:
    0. ( 1.797 , 2.36 ] (1.797, 2.36] (1.797,2.36]
    1. ( 2.36 , 2.92 ] (2.36, 2.92] (2.36,2.92]
    2. ( 2.92 , 3.48 ] (2.92, 3.48] (2.92,3.48]
    3. ( 3.48 , 4.04 ] (3.48, 4.04] (3.48,4.04]
    4. ( 4.04 , 4.6 ] (4.04, 4.6] (4.04,4.6]
  • 若 只取序号的话,如下
pd.cut(df['exercise_price'],5,labels=False)
0       0
1       1
2       1
3       2
4       2
       ..
2517    2
2518    2
2519    2
2520    2
2521    2
Name: exercise_price, Length: 2522, dtype: int64
pd.cut(df['exercise_price'],5,labels=False).unique()
array([0, 1, 2, 3, 4], dtype=int64)

idxmax() & idxmin() 返回最大(小)值第一个索引

  • 从函数名就可以直观地看出这两个函数的作用:返回最大值与最小值的函数索引。

  • 作用返回最大值和最小值第一个索引

  • 我们一般使用以下的代码去获取 最小与最大值的第一个索引

# 普通取最小值的第一个索引
df[df['exercise_price']==df['exercise_price'].min()].index[0]
146
  • 使用 idxmin()的话
# idxmin() 取最小值的第一个索引
df['exercise_price'].idxmin()
146
  • idxmax() 与 idxmin() 类似。
  • 由于它只取第一个最小(大)值,一般来说,我们都要取所有最小(大)值的索引。实话来说,使用这函数的机会确实不是很大,虽然看着是更加简洁与高效。

nsmallest() & nlargest() 取n个最小(大)值

  • 取n个最小(大)值
df['exercise_price'].nsmallest(100)
146     1.8
302     1.8
381     1.8
639     1.8
763     1.8
       ... 
1256    2.0
1471    2.0
1505    2.0
1541    2.0
1577    2.0
Name: exercise_price, Length: 100, dtype: float64
df[['exercise_price','ts_code']].nsmallest(5,'exercise_price')
exercise_pricets_code
1461.810000384.SH
3021.810000568.SH
3811.810000396.SH
6391.810000393.SH
7631.810000567.SH

pivot_table 数据透视表

  • 创建一个电子表格样式的数据透视表

  • 例如:我们创建一个 各个价格的挂牌基准价的平均数

df.pivot_table(index='exercise_price',columns='call_put',values='list_price',aggfunc='mean')
call_putCP
exercise_price
1.8000.1901710.163243
1.8500.1621600.158540
1.9000.1337500.181680
1.9080.1789000.080900
1.9500.1659330.136525
.........
4.2000.0967380.291938
4.3000.0681880.350062
4.4000.0576170.375683
4.5000.0491170.426150
4.6000.0531670.456633

102 rows × 2 columns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值