【问题标题】:count number of positive consecutive positive and negativecount 正数连续正负数
【发布时间】:2018-02-16 05:48:36
【问题描述】:

我真的被困住了。我有一个数据框,其列如下所示

Dailychange:
1
2
3
0
-1
-2
-3
1
2

我想将正负数连续计算到两个列表中,输出为 pos[3,2] nutral[1] neg[3]。我试过用一个简单的循环来解决它,比如

    # for i in symbol:
    #     if (symbol['Dailychange']>0):
    #         counter+=1
    #         cons_list.append(counter)
    #     else:
    #         counter=0
    #         cons_list.append(counter)
    # print(cons_list)

由于我的 if 语句,这会输出错误。然后我尝试使用 where 函数

symbol['positive']=symbol.where(symbol['Dailychange']>0,'positive','Negative')

那也没有成功。非常感谢您在这方面的帮助。

【问题讨论】:

标签: python-3.x pandas pandas-groupby


【解决方案1】:

这里需要一个新的段落,我使用np.where创建的

df['New']=np.where(df['Num']>0,'positive',np.where(df['Num']==0,'Nutral','Negative'))
s=df.groupby([df['New'],(df['New']!=df['New'].shift()).cumsum()]).size().reset_index(level=1,drop=True)
s
Out[41]: 
New
Negative    3
Nutral      1
positive    3
positive    2
dtype: int64

更多信息

(df['New']!=df['New'].shift()).cumsum()
Out[804]: 
0    1
1    1
2    1
3    2
4    3
5    3
6    3
7    4
8    4
Name: New, dtype: int32

(df['New']!=df['New'].shift())
Out[805]: 
0     True 
1    False
2    False
3     True # here is the status change 
4     True # here is the status change 
5    False  # those one do not change should carry over the same number as before 
6    False
7     True # here is the status change 
8    False
Name: New, dtype: bool

我们将连续的正面或负面视为一组,一旦他们改变了他们就会进入下一组

还有一件事 True + False =1

【讨论】:

  • 您好,非常感谢您的回答。我得到第一行,关键是使用 numpy 而不是 pandas。但我对第二行的语法感到困惑。 cumsum 是如何发挥作用的。再次感谢。
  • @Longroadahead (df['New']!=df['New'].shift()).cumsum() ,这是为了找到连续的:-),想想数字1, 1,1,2,2,2,3,3,3,1,1,1,找到重复模式的组将是000111222333
  • 由于某种原因我无法理解它,我的理解是这样的。 igroup by df['New'] 并且条件是(df['New'] 不等于累积和的下一个内联)然后 .size 找到元素的数量,reset_index 将它们放在一起。我只是没有得到累积和部分。非常感谢您对此的帮助!
  • @Longroadahead 添加更多信息
  • @Longroadahead 是的,例如 T F T F , cumsum 将是 1122,它们只会将 True 值与平均值相加,如果它是 False 它将结转之前的值(它们在同一组)
【解决方案2】:

pd.cut 和 groupby 正是您正在寻找的 -

import numpy as np
import pandas as pd
x = pd.DataFrame([1, 2, 3, 0, -1, -2, -3, 1, 2],columns=['Dailychange'])
col = x['Dailychange']
x['Labels'] = list(pd.cut(x['Dailychange'],[-float("inf"),-0.1,0.1,float("inf")],labels=['neg','neutral','pos']))
# for i,e in enumerate(x['Labels']):
#     print(col[i],x['Labels'][i])
x['chunk_number'] = (x['Labels'] != x['Labels'].shift()).cumsum()
grouped_df = x.groupby('chunk_number')
for i in grouped_df.groups.keys():
    print(list(grouped_df.get_group(i)['Dailychange']))

也结帐:Documentation | Related question | Another Related question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 2017-01-31
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多