我创建了一个包含两列的Dataframe,并希望根据其他数组的值计数来附加它们。
cols = ['count_pos','count_neg']
df_count = pd.DataFrame(columns=cols)
我的数组y的值为y = [1,-1,-1,1,1,1,1,-1,-1]
现在我想更新y中每个值的更改,计算这些事件并附加到相应的列。
for i in range(1,10):
if y[i] == -1:
print(y[i])
if count_pos > 0:
df_count.loc['count_pos'].append = count_pos
count_pos = 0
count_neg = count_neg - 1
else:
if count_neg< 0:
print(count_neg)
df_count.loc['count_neg'].append = count_neg
count_neg = 0
count_pos = count_pos + 1
但是我没有得到结果。请让我知道如何将值附加到dataframe列。
我想要的输出是 df_count
count_pos count_neg
1 -2
4 -2
答案 0 :(得分:1)
使用groupby
计算连续的正/负值组:
s = pd.Series(y)
v = s.gt(0).ne(s.gt(0).shift()).cumsum()
pd.DataFrame(
v.groupby(v).count().values.reshape(-1, 2), columns=['pos', 'neg']
)
pos neg
0 1 2
1 4 2
答案 1 :(得分:0)
改编自@cs95 的回答:
a = pd.Series([-1, 2, 15, 3, 45, 5, 23, 0, 6, -4, -8, -5, 3,
-9, -7, -36, -71, -2, 25, 47, -8])
def pos_neg_count(a):
v = a.ge(0).ne(a.ge(0).shift()).cumsum()
vals = v.groupby(v).count().values
cols = ['pos', 'neg'] if a[0] >= 0 else ['neg', 'pos']
try:
result = pd.DataFrame(vals.reshape(-1, 2), columns=cols)
except ValueError:
vals = np.insert(vals, len(vals), 0)
result = pd.DataFrame(vals.reshape(-1, 2), columns=cols)
return result
pos_neg_count(a)
# neg pos
# 0 1 8
# 1 3 1
# 2 5 2
# 3 1 0
我认为,这将处理被重塑的数组具有奇数的情况。元素。