简体   繁体   English

如何比较两列并获取 python pandas dataframe 中两列中所有匹配项的第三列的平均值?

[英]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.

相关问题 Python Pandas - 检查两列中的值,对第三列求和 - Python Pandas - check value in two columns, sum the 3rd column 第 3 列 pandas python 中至少有两列 - Minimum of two columns in a 3rd column pandas python 如何基于两列删除重复数据,从而删除熊猫数据框中第三列中最大的列? - How to remove duplicates based on two columns removing the the largest of 3rd column in pandas dataframe? 比较两个日期列 - 检查它们是否在范围内 - 从第三列取值 - compare two date columns - check if they fall in range - take value from 3rd column 熊猫按两列分组,并从第三列输出值 - Pandas groupby two columns and output values from 3rd column 比较两个数据帧,只获取索引和列名不匹配的值 - compare two data frames and get only non matching values with index and column names pandas dataframe python 如何比较 dataframe 中的两列并根据匹配字段更新列 - how to compare two columns in dataframe and update a column based on matching fields Python Pandas:排序和分组,然后对第二列的两个连续行求和,得出第三列的特定值 - Python Pandas: Sort and group by, then sum two consecutive rows of 2nd column for a specific value of a 3rd column Python 或 Excel:如何比较 2 列,然后在新列中写入第 3 列的值? - Python or Excel: How can you compare 2 columns and then write the value of a 3rd column in a new column? Python pandas lambda 比较 Z6A8064B5DF479455500553C47DCZ 中的两列 - Python pandas lambda compare two columns in dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM