Python 3.x 一列中每个条目的平均值基于另一列的唯一值
我有一个文件,看起来像:Python 3.x 一列中每个条目的平均值基于另一列的唯一值,python-3.x,for-loop,pandas-groupby,Python 3.x,For Loop,Pandas Groupby,我有一个文件,看起来像: E1 32 45 + Apple E2 54 76 + Apple ... ... -E2 300 400 + Apple -E1 540 760 + Apple E1 560 600 - Orange E2 340 440 - Orange ... ... -E2 30 40 - Orange -E1 20 7 -
E1 32 45 + Apple
E2 54 76 + Apple
...
...
-E2 300 400 + Apple
-E1 540 760 + Apple
E1 560 600 - Orange
E2 340 440 - Orange
...
...
-E2 30 40 - Orange
-E1 20 7 - Orange
此处,最后一列中每个唯一值的E的范围为1到100。最后一列可以看到几千种独特的水果。我想从最后一列的每个唯一值估计第一个E(E1)的差值的平均值
E1 ((45-32)+(600-560))/2 = 26.5
E2 ((76-54)+(440-340))/2 = 61
我要计算E1,E2和E3,还有-E3,-E2,-E1,其中-E1是最后一列中每个唯一条目的最后一个E,类似地-E2和-E3是第二个和第三个最后的E
我尝试了熊猫群比来解决这个问题:
df1.groupby(str(line[4]))[line[2]-line[1]].mean()
我不知道groupby是否是正确的方法,我很难对这个案例进行循环` 您是否已经在数据帧(即pd.dataframe())中包含此数据?有列名吗?是的,我有,假设列名是A1、A2、A3、A4和A5。