小编Ros*_*ose的帖子

按总和条件分组

我有以下df,我想按日期和参考分组,但总和条件.

在这方面,我需要按日期和参考进行分组,并且仅当P>> = PP时才加'Q'列.

df = DataFrame({'Date' : ['1', '1', '1', '1'],
                'Ref' : ['one', 'one', 'two', 'two'],
                'P' : ['50', '65', '30', '38'],
                'PP' : ['63', '63', '32', '32'],
                'Q' : ['10', '15', '20', '10']})

df.groupby(['Date','Ref'])['Q'].sum() #This does the right grouping byt summing the whole column
df.loc[df['P'] >= df['PP'], ('Q')].sum() #this has the right sum condition, but does not divide between Date & Ref
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?提前谢谢了

python group-by loc python-3.x pandas

2
推荐指数
1
解决办法
80
查看次数

如何用矩阵A *(X + B)= C(不平方)求解方程

在python中可以解决A*x=B。但是是否有可能解决 A*(B+x)=Cx是未知向量和x1 = 0的问题,从而使我们有2个方程和2个未知值并平方?

A= np.array([[1,2,3],[3,2,1]])
B= np.array([10,20,-10])
C= np.array([0,0])

¿XX= np.array([0,1,1]) >> so that we only solve x2 and x3?

Solution: x = array([0, -40, 20])
Run Code Online (Sandbox Code Playgroud)

我知道我可以做类似A * x = B的事情,但是我想更直接地做到这一点:

A=np.array([[2,3],[2,1]])
B=np.array([-10,-30])
x=np.linalg.solve(A,B)

array([-20.,  10.])
Run Code Online (Sandbox Code Playgroud)

总而言之,我想要一个X矢量,它是(0,x2,x3),因此它成为可以求解的方程,但仍然不知道如何使用np.linalg.solve()来实现。然后系统将如下所示:

(10+x1) +( 40+2x2) - (30+3x3) = 0
(30+3x1) + (40+2x2) - (10+x3) = 0

10 + ( 40+2x2) - (30+3x3) = 0
30 + (40+2x2) - (10+x3) = 0
Run Code Online (Sandbox Code Playgroud)

只解决x2和x3

python arrays numpy matrix

-2
推荐指数
1
解决办法
93
查看次数

标签 统计

python ×2

arrays ×1

group-by ×1

loc ×1

matrix ×1

numpy ×1

pandas ×1

python-3.x ×1