首页 > 解决方案 > 识别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

那可能吗 ?

非常感谢您提前。

干杯,杰斯珀。

标签: pythondataframerows

解决方案


您可以将mergewith用于具有相同值的索引值的reset_index列,例如,如果没有参数,它也会被所有列合并:indexAllDTradesSpecificTradeon

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

推荐阅读