[英]how to compare two columns and get the mean value of the the 3rd column for all matching items in the two in python pandas dataframe?
I have the following table named Rides :我有下表名为Rides :
start_id start_id | end_id end_id | eta埃塔 |
---|---|---|
A一个 | B乙 | 5 5 |
B乙 | C C | 4 4 |
A一个 | C C | 6 6 |
A一个 | B乙 | 5 5 |
B乙 | A一个 | 3 3 |
C C | A一个 | 3 3 |
B乙 | C C | 6 6 |
C C | A一个 | 5 5 |
A一个 | B乙 | 8 8 |
From the Rides Table, I want to Create a new table which should look like something like below:从Rides表中,我想创建一个新表,如下所示:
start_id start_id | end_id end_id | mean _eta意思是_eta |
---|---|---|
A一个 | B乙 | 6 ((5+5+8)/3) 6 ((5+5+8)/3) |
B乙 | C C | 5 ((4+6)/2)) 5 ((4+6)/2)) |
A一个 | C C | 6 6 |
B乙 | A一个 | 3 3 |
C C | A一个 | 4 ((3+5)/2)) 4 ((3+5)/2)) |
so mean_eta of 1st row is returning 8 as there are three matching rides between start_ID = "A" and end_ID = "B" with eta 5,5,8 , so the mean_eta = (5+5+8)/3 = 6 How should I do it?所以第一行的 mean_eta 返回 8 因为 start_ID = "A"和 end_ID = "B"之间有三个匹配的游乐设施,eta 5,5,8 ,所以mean_eta = (5+5+8)/3 = 6如何我应该这样做吗? Please help.请帮忙。
groupby and get the aggregate mean. groupby 并获得总平均值。 Code below;代码如下;
df.groupby(['start_id','end_id'])['eta'].agg('mean').to_frame('eta-mean')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.