Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 新版本:如何按具有不同聚合统计信息的所有列进行分组_Python_Pandas - Fatal编程技术网

Python 新版本:如何按具有不同聚合统计信息的所有列进行分组

Python 新版本:如何按具有不同聚合统计信息的所有列进行分组,python,pandas,Python,Pandas,我有一个df,看起来像这样: time volts1 volts2 0 0.000 -0.299072 0.427551 2 0.001 -0.299377 0.427551 4 0.002 -0.298767 0.427551 6 0.003 -0.298767 0.422974 8 0.004 -0.298767 0.422058 10 0.005 -0.298462 0.422363 12 0.006 -0.298767 0.42

我有一个df,看起来像这样:

     time    volts1    volts2
0   0.000 -0.299072  0.427551
2   0.001 -0.299377  0.427551
4   0.002 -0.298767  0.427551
6   0.003 -0.298767  0.422974
8   0.004 -0.298767  0.422058
10  0.005 -0.298462  0.422363
12  0.006 -0.298767  0.422668
14  0.007 -0.298462  0.422363
16  0.008 -0.301208  0.420227
18  0.009 -0.303345  0.418091
实际上,df有>50列,但为了简单起见,我只显示了3列

我想每n行按这个df分组,比如说5行。我想用
max
聚合
time
,其余的列我想用
mean
聚合。因为有这么多列,我希望能够循环这个,而不必手动执行

我知道我可以这样做,手动浏览并创建所有新列:

df.groupby(df.index // 5).agg(time=('time', 'max'),
                           volts1=('volts1', 'mean'),
                           volts1=('volts1', 'mean'),
                           ...
                           )
但是因为有这么多的列,我想在循环中这样做,比如:

df.groupby(df.index // 5).agg(time=('time', 'max'),
                           # df.time is always the first column
                           [i for i in df.columns[1:]]=(i, 'mean'),
                           )
如果有用:

print(pd.__version__)
1.0.5

您可以使用字典:

d = {col: "mean" if not col=='time' else "max" for col in df.columns}
#{'time': 'max', 'volts1': 'mean', 'volts2': 'mean'}
df.groupby(df.index // 5).agg(d)

    time    volts1    volts2
0  0.002 -0.299072  0.427551
1  0.004 -0.298767  0.422516
2  0.007 -0.298564  0.422465
3  0.009 -0.302276  0.419159