pandas 分离pandas DataFrame的正负值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35204716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:37:31  来源:igfitidea点击:

Separating positive and negative values of pandas DataFrame

pythonpandasdataframe

提问by David Hancock

I need to separately sum all positive and negative values in a column ie

我需要分别对列中的所有正值和负值求和,即

pos_values = [x for x in df.prediction_at_ohlcv_end_date if x > 0] 
neg_values = [x for x in df.prediction_at_ohlcv_end_date if x < 0] 

Here's a data sample

这是一个数据样本

market_trading_pair next_future_timestep_return ohlcv_start_date    prediction_at_ohlcv_end_date
0   Poloniex_ETH_BTC    0.003013    1450753200  -0.157053
1   Poloniex_ETH_BTC    -0.006521   1450756800  -0.920074
2   Poloniex_ETH_BTC    0.003171    1450760400  0.999806
3   Poloniex_ETH_BTC    -0.003083   1450764000  0.627140
4   Poloniex_ETH_BTC    -0.001382   1450767600  0.999857

What's a nice way to do this in pandas ?

在Pandas中这样做的好方法是什么?

EDIT:

编辑:

I have been able to do this thanks to some helpful stackers, I realised I can't a futher calculation however. `

多亏了一些有用的堆垛机,我才能够做到这一点,但我意识到我无法进一步计算。`

if prediction_at_ohlcv_end_date > 0 : 
return = prediction_at_ohlcv_end_date * next_future_timestep_return. 

For each element in the frame, Any ideas?`

对于框架中的每个元素,有什么想法吗?`

回答by Anton Protopopov

You could use method sumof pandas.Seriesfor your particular column:

你可以使用方法sumpandas.Series为您的特定列:

neg = df.prediction_at_ohlcv_end_date[df.prediction_at_ohlcv_end_date < 0].sum()
pos = df.prediction_at_ohlcv_end_date[df.prediction_at_ohlcv_end_date >= 0].sum()

In [51]: pos
Out[51]: 2.6268029999999998

In [52]: neg
Out[52]: -1.077127

For your values:

对于您的价值观:

pos_values = df.prediction_at_ohlcv_end_date[df.prediction_at_ohlcv_end_date >= 0]
neg_values = df.prediction_at_ohlcv_end_date[df.prediction_at_ohlcv_end_date < 0]

EDIT

编辑

For your edit you could do:

对于您的编辑,您可以执行以下操作:

mask = df.prediction_at_ohlcv_end_date >= 0
res = df.prediction_at_ohlcv_end_date[mask] * df.next_future_timestep_return[mask]

In [10]: res
Out[10]: 
2    0.003170
3   -0.001933
4   -0.001382
dtype: float64