gpt4 book ai didi

python - 从 Pandas DataFrame 中消除重复条目

转载 作者:行者123 更新时间:2023-12-02 16:20:59 24 4
gpt4 key购买 nike

我有一个看起来像这样的 pandas DataFrame:

Date       positions      price                   
2009-03-03 buy 3.156071
2009-12-10 buy 7.015357
2010-02-02 buy 6.995000
2010-03-04 sell 7.525357
2013-09-24 buy 17.467857
2013-10-08 buy 17.176428
2014-01-16 buy 19.794643
2014-01-28 buy 18.089285
2014-04-02 sell 19.376785

这只是 DataFrame 的一个片段,但我想要做的是在位置列中包含“卖出”的两行之间的位置列中只有一行包含“买入”。换句话说,我想消除在初始购买信号之后重复出现的购买信号。

我想消除第一个已经发生的重复购买信号。因此,这意味着预期的输出,给定第一个数据帧将是:

Date       positions      price                 
2009-03-03 buy 3.156071
2010-03-04 sell 7.525357
2013-09-24 buy 17.467857
2014-04-02 sell 19.376785

最佳答案

不清楚您希望分组的buy 是什么。我选择了 sum 但也许你想要 mean

import pandas as pd

df = pd.DataFrame({'Date': {0: '2009-03-03',
1: '2009-12-10',
2: '2010-02-02',
3: '2010-03-04',
4: '2013-09-24',
5: '2013-10-08',
6: '2014-01-16',
7: '2014-01-28',
8: '2014-04-02'},
'positions': {0: 'buy',
1: 'buy',
2: 'buy',
3: 'sell',
4: 'buy',
5: 'buy',
6: 'buy',
7: 'buy',
8: 'sell'},
'price': {0: 3.156071,
1: 7.015357000000001,
2: 6.995,
3: 7.5253570000000005,
4: 17.467857000000002,
5: 17.176428,
6: 19.794643,
7: 18.089285,
8: 19.376785}})


df['g'] = (df['positions']=='sell').cumsum()
df = df.groupby(['g','positions']).sum().reset_index()
df.sort_values(by=['g','positions'], ascending=[True,False], inplace=True)

df[['positions','price']]

输出

   positions    price
0 buy 17.166428
2 sell 7.525357
1 buy 72.528213
3 sell 19.376785

关于python - 从 Pandas DataFrame 中消除重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65523924/

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