python - 识别python DataFrame中相等的行...
问题描述
我有一个带有很多安全交易的大型 DataFrame(称为 AllDTrades),比如这个:
trd_exctn_dt ascii_rptd_vol_tx rptd_pr yld_pt sttl_dt
1 2018-07-02 150000.0 98.6100 4.476914 7/5/2018
....
现在,我需要找到一个函数,它能够告诉我特定交易在 DataFrame 中出现了多少次以及在 DataFrame 中的位置。所以我需要问这个问题(这当然行不通):
AllDTrades.loc[AllDTrades==SpecificTrade],
在哪里
SpecificTrade 是一种交易,可以这样说:
2018-07-02 150000.0 98.6100 4.476914 7/5/2018
因此,我需要确定 SpecificTrade 1) 是否属于 AllDTrades 2) AllDTrades 中的交易位置 3) 此外,如果 AllDTrades 中存在多个交易,例如 SpecificTrade,我需要知道所有交易的位置在 AllDTrades
那可能吗 ?
非常感谢您提前。
干杯,杰斯珀。
解决方案
您可以将merge
with用于具有相同值的索引值的reset_index
列,例如,如果没有参数,它也会被所有列合并:index
AllDTrades
SpecificTrade
on
print (AllDTrades)
trd_exctn_dt ascii_rptd_vol_tx rptd_pr yld_pt sttl_dt
1 2018-07-02 150000.0 98.61 4.476914 7/5/2018
2 2018-07-03 290000.0 98.61 4.476914 7/5/2018
3 2018-07-02 150000.0 98.61 4.476914 7/5/2018
SpecificTrade = AllDTrades.iloc[[0]]
print (SpecificTrade)
trd_exctn_dt ascii_rptd_vol_tx rptd_pr yld_pt sttl_dt
1 2018-07-02 150000.0 98.61 4.476914 7/5/2018
df = AllDTrades.reset_index().merge(SpecificTrade)
print (df)
index trd_exctn_dt ascii_rptd_vol_tx rptd_pr yld_pt sttl_dt
0 1 2018-07-02 150000.0 98.61 4.476914 7/5/2018
1 3 2018-07-02 150000.0 98.61 4.476914 7/5/2018
vals = df['index']
print (vals)
0 1
1 3
Name: index, dtype: int64
print (AllDTrades.loc[vals])
trd_exctn_dt ascii_rptd_vol_tx rptd_pr yld_pt sttl_dt
1 2018-07-02 150000.0 98.61 4.476914 7/5/2018
3 2018-07-02 150000.0 98.61 4.476914 7/5/2018
推荐阅读
- excel - Excel 宏/VBA - 复制选定的行并添加重复值
- ubuntu - 如何在 Ubuntu 上彻底清除 Visual Studio Code?
- html - 在 HTML 中保持按钮对齐
- coq - 在 coq 中推广一组证明
- magento - Magento 2 导入产品属性类型固定产品税
- c - 尝试使用解密时间来解密代码
- python - keras 中的截断反向传播,每批一个序列
- java - Spring boot Jasper 报告:Java bean 作为数据源
- qt - 如何在 QSGNode 中添加新节点
- reportlab - reportlab pdfmetrics ttfont postscript错误