【问题标题】:How do i get the sum of a dataframe with a specified conditions?如何获得具有指定条件的数据框的总和?
【发布时间】:2020-03-09 04:56:52
【问题描述】:

我有以下数据框标题: Instrument ClosedVolume NetRevenue TradePnL

在我的交易数据中,我多次交易同一工具导致不同的 TradePnL。

我想总结一下,或者如果可能的话,重申我数据框中的所有不同工具。

例如,我想获取我的阿里巴巴的所有交易数据,使TradePnL为正,我应该怎么做?

    sum(df['TradePnL'] ) #should give me my tradePnL of all my trades

    df[df['Instrument'] == "Alibaba"] #this will give me all instruments data with Alibaba

【问题讨论】:

    标签: python arrays pandas numpy dataframe


    【解决方案1】:

    只需将条件与二进制和

    df[(df['Instrument'] == "Alibaba") & (df['TradePnL'] > 0)]

    我猜……

    更有可能

    df[df['TradePnL'] > 0].groupby(['Instrument']).agg("sum")
    

    选择哪里PNL > 0,然后按仪器分组,最后得到正pnls的总和

    【讨论】:

    • 谢谢 Joran,df[df['TradePnL'] > 0].groupby(['Instrument']).agg("sum") #正是我想要的!
    • 如何按降序排列?
    • df[df['TradePnL'] > 0].groupby(['Instrument']).agg("sum").sort_values(by ='TradePnL',ascending=False)跨度>
    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 2020-09-05
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多