gpt4 book ai didi

python - 识别 python DataFrame 中相等的行...

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:43 24 4
gpt4 key购买 nike

我有一个大型 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

所以,我需要查明 S​​pecificTrade1) 是 AllDTrades 的一部分2)AllDTrades中的交易位置3) 另外,如果 AllDTrades 中有多个交易,例如 SpecificTrade,我需要知道所有交易在 AllDTrades 中的位置

这可能吗?

非常感谢您。

干杯,杰斯珀。

最佳答案

您可以使用 mergereset_index对于 AllDTrades 的索引值列 indexSpecificTrade 等值,如果没有参数 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

关于python - 识别 python DataFrame 中相等的行...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53132473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com