请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365811 新帖:18

使用qgrid和鼠标点点点完成dataframe的排序,过滤和编辑

吃瓜群众小王发表于:5 月 10 日 00:58回复(1)

qgrid是一个notebook中编辑dataframe的插件

你可以点击列名排序,过滤DataFrame数据,还可以双击编辑数据。
因为qgrid依赖python内核,因此需要将notebook克隆到研究内执行后才会有完整的显示。

底下是在我研究里的截图
qgrid4.png

qgrid是一个notebook的一个插件¶

它使用SlickGrid在notebook中直观的显示pandas DataFrame。
你可以点击列名排序,过滤DataFrame数据,还可以双击编辑数据。

import qgrid
szzs = get_price("000001.XSHG")
hs300 = get_price("000300.XSHG")

qgrid_szzs = qgrid.show_grid(szzs)
qgrid_hs300 = qgrid.show_grid(hs300)
#显示单条
qgrid_szzs
# 双击编辑数据
# 单击列名排列数据
# 单击列名右侧图标筛选数据
# 修改2015-01-05的开盘价为0
{'name': 'sort_changed', 'old': {'column': None, 'ascending': True}, 'new': {'column': 'high', 'ascending': True}}
{'name': 'sort_changed', 'old': {'column': 'high', 'ascending': True}, 'new': {'column': 'index', 'ascending': True}}

获取修改后的dataframe¶

qgrid不修改源dataframe

qgrid_szzs.get_changed_df()
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
open close high low volume money
2015-01-05 0.000 3350.519 3369.281 3253.883 5.313524e+10 5.497601e+11
2015-01-06 3330.799 3351.446 3394.224 3303.184 5.016617e+10 5.323985e+11
2015-01-07 3326.649 3373.954 3374.896 3312.211 3.919189e+10 4.364167e+11
2015-01-08 3371.957 3293.456 3381.566 3285.095 3.711312e+10 3.992303e+11
2015-01-09 3276.965 3285.412 3404.834 3267.509 4.102409e+10 4.586480e+11
2015-01-12 3258.213 3229.316 3275.185 3191.582 3.220647e+10 3.662731e+11
2015-01-13 3223.542 3235.301 3259.386 3214.412 2.307258e+10 2.735888e+11
2015-01-14 3242.337 3222.437 3268.483 3193.978 2.401907e+10 2.672045e+11
2015-01-15 3224.070 3336.455 3337.084 3207.545 2.825462e+10 3.306105e+11
2015-01-16 3343.603 3376.495 3400.318 3340.490 3.398768e+10 3.922539e+11
2015-01-19 3189.727 3116.351 3262.207 3095.066 4.010988e+10 4.098860e+11
2015-01-20 3114.560 3173.052 3190.245 3100.477 3.570808e+10 4.162952e+11
2015-01-21 3189.085 3323.611 3337.004 3178.343 4.109560e+10 4.737587e+11
2015-01-22 3327.319 3343.344 3352.384 3293.978 3.533830e+10 4.078741e+11
2015-01-23 3357.096 3351.764 3406.786 3328.293 3.662492e+10 4.209795e+11
2015-01-26 3347.257 3383.182 3384.798 3321.315 3.175410e+10 3.584275e+11
2015-01-27 3389.853 3352.960 3390.216 3290.219 3.745176e+10 4.182989e+11
2015-01-28 3325.722 3305.738 3354.802 3294.655 3.019271e+10 3.415643e+11
2015-01-29 3258.997 3262.305 3286.786 3234.241 2.746586e+10 2.964245e+11
2015-01-30 3273.747 3210.363 3288.503 3210.308 2.583125e+10 2.842657e+11
2015-02-02 3148.136 3128.300 3175.134 3122.572 2.508616e+10 2.668500e+11
2015-02-03 3156.086 3204.907 3207.935 3129.732 2.481922e+10 2.833560e+11
2015-02-04 3212.822 3174.126 3238.982 3171.144 2.490981e+10 2.901552e+11
2015-02-05 3251.212 3136.531 3251.212 3135.819 3.061393e+10 3.482670e+11
2015-02-06 3120.090 3075.907 3129.539 3052.939 2.467497e+10 2.665028e+11
2015-02-09 3063.510 3095.124 3119.034 3049.111 2.061084e+10 2.407197e+11
2015-02-10 3090.490 3141.593 3142.099 3084.253 1.938171e+10 2.250849e+11
2015-02-11 3145.765 3157.704 3166.421 3139.052 1.728401e+10 2.108625e+11
2015-02-12 3157.959 3173.416 3181.766 3134.244 1.945923e+10 2.296916e+11
2015-02-13 3186.808 3203.827 3237.159 3182.794 2.612904e+10 2.930177e+11
... ... ... ... ... ... ...
2015-11-20 3620.791 3630.500 3640.529 3607.915 3.108020e+10 4.139109e+11
2015-11-23 3630.867 3610.320 3654.754 3598.871 3.159975e+10 4.141482e+11
2015-11-24 3602.887 3616.113 3616.484 3563.104 2.488105e+10 3.277585e+11
2015-11-25 3614.068 3647.930 3648.367 3607.518 2.730249e+10 3.808029e+11
2015-11-26 3659.574 3635.552 3668.376 3629.865 3.067616e+10 4.262474e+11
2015-11-27 3616.544 3436.303 3621.897 3412.427 3.542875e+10 4.643112e+11
2015-11-30 3433.855 3445.405 3470.371 3327.812 3.041979e+10 3.875037e+11
2015-12-01 3442.441 3456.309 3483.414 3417.545 2.523908e+10 3.302567e+11
2015-12-02 3450.278 3536.905 3538.847 3427.661 3.014915e+10 3.691830e+11
2015-12-03 3525.727 3584.824 3591.730 3517.230 2.811113e+10 3.388591e+11
2015-12-04 3558.149 3524.992 3568.970 3510.412 2.517364e+10 3.197668e+11
2015-12-07 3529.806 3536.927 3543.945 3506.625 2.083026e+10 2.805616e+11
2015-12-08 3518.648 3470.070 3518.648 3466.790 2.243673e+10 2.978217e+11
2015-12-09 3462.583 3472.439 3495.702 3454.879 1.956988e+10 2.678549e+11
2015-12-10 3469.806 3455.495 3503.654 3446.273 2.004275e+10 2.794997e+11
2015-12-11 3441.597 3434.581 3455.548 3410.924 1.829089e+10 2.450764e+11
2015-12-14 3403.507 3520.668 3521.779 3399.280 2.153746e+10 2.792135e+11
2015-12-15 3518.126 3510.354 3529.961 3496.854 2.004713e+10 2.762749e+11
2015-12-16 3522.091 3516.187 3538.689 3506.291 1.934823e+10 2.652887e+11
2015-12-17 3533.628 3579.999 3583.408 3533.628 2.838565e+10 3.814396e+11
2015-12-18 3574.940 3578.964 3614.698 3568.161 2.737079e+10 3.653858e+11
2015-12-21 3568.581 3642.472 3651.056 3565.751 2.998493e+10 3.983170e+11
2015-12-22 3645.990 3651.767 3652.635 3616.870 2.611788e+10 3.608460e+11
2015-12-23 3653.282 3636.089 3684.567 3633.025 2.982018e+10 4.199029e+11
2015-12-24 3631.313 3612.485 3640.221 3572.277 2.277852e+10 3.154213e+11
2015-12-25 3614.054 3627.914 3635.256 3601.738 1.984511e+10 2.746600e+11
2015-12-28 3635.769 3533.779 3641.588 3533.779 2.699833e+10 3.690428e+11
2015-12-29 3528.395 3563.736 3564.171 3515.520 1.825519e+10 2.509389e+11
2015-12-30 3566.731 3572.876 3573.684 3538.112 1.878896e+10 2.677877e+11
2015-12-31 3570.467 3539.182 3580.602 3538.353 1.769637e+10 2.540311e+11

244 rows × 6 columns

事件绑定¶

可以通过on接口绑定事件处理函数到qgrid,当dataframe发生变化时执行对应的处理函数
事件列表如下

[
    'instance_created',
    'cell_edited',
    'selection_changed',
    'viewport_changed',
    'row_added',
    'row_removed',
    'filter_dropdown_shown',
    'filter_changed',
    'sort_changed',
    'text_filter_viewport_changed',
    'json_updated'
]
def deal_sort(event, qgrid_widget):
    print(event)
qgrid_szzs.on('sort_changed',deal_sort)
# 现在点击排序,会在dataframe下方显示事件内容

设置默认用qgrid显示datafame和series¶

该功能不完善,没法拿到修改后的数据,不推荐

qgrid.enable()
get_price("000001.XSHG")
# 关闭qgrid显示datafame和series
qgrid.disable()
#显示多条
from IPython.display import display
display(qgrid_szzs)
display(qgrid_hs300)
 

全部回复

0/140

量化课程

    移动端课程