[英]Mean of each column of multiple dataframes to a dataframe - Python pandas
I have a dataframe df as follows: 我有一个数据框df,如下所示:
A B C
1 2 3
2 1 2
3 3 1
And I would like the mean of every column and make a dataframe with it. 我想要每一列的均值,并用它制作一个数据框。 That would be in this example:
在此示例中将是:
A B C
2 2 2
The code I did was: 我做的代码是:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 3), columns=list('ABC')) # To create df
dfs = np.array_split(df.sample(frac=1),4) # Split it in 4
daf = []
for i in range(len(dfs):
daf.append(dfs[i].mean())
daf.to_frame()
However I am unable to make it work. 但是我无法使其工作。
Use mean
, but because it return Series
use to_frame
and transpose: 使用
mean
,但是因为它返回Series
使用to_frame
并转置:
df = df.mean().to_frame().T
print (df)
A B C
0 2.0 2.0 2.0
Or: 要么:
df = pd.DataFrame([df.mean()])
print (df)
A B C
0 2.0 2.0 2.0
For multiple DataFrames
: 对于多个
DataFrames
:
daf = []
for i in dfs:
daf.append(i.mean().to_frame().T)
print (daf[0])
A B C
0 -0.92493 1.022305 1.52295
what is same as list comprehension
solution: 什么与
list comprehension
解决方案相同:
daf = [i.mean().to_frame().T for i in dfs]
the method of dataframe, 'apply' will be good. 数据框的方法“应用”会很好。 the code is below.
代码如下。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 3), columns=list('ABC')) # To create df
df.apply(lambda x: np.mean(x), axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.