pandas:计算列中每行的numpy数组的平均值

时间:2021-10-25 21:23:13

I have a pandas dataframe, df , that contains columns where each row contains a numpy array of varying size e.g.

我有一个pandas数据帧,df,其中包含每列包含不同大小的numpy数组的列,例如

   column A 
0  np.array([1,2,3])
1  np.array([1,2,3,4])
2  np.array([1,2])

I there a built in pandas function that will return the mean value of each array, i.e. row, for the entire column? Something like :

我有一个内置的pandas函数,它将返回整个列的每个数组的平均值,即行?就像是 :

df.A.mean()

But which operates on each row. Thanks for any help.

但是哪一行都在运行。谢谢你的帮助。

1 个解决方案

#1


You can use df.<column>.map to apply a function to each element in a column:

您可以使用df。 .map将函数应用于列中的每个元素:

df = pd.DataFrame({'a': 
    [np.array([1, 2, 3]), 
     np.array([4, 5, 6, 7]), 
     np.array([7, 8])]
})

df
Out[8]: 
              a
0     [1, 2, 3]
1  [4, 5, 6, 7]
2        [7, 8]

df['a'].map(lambda x: x.mean())
Out[9]: 
0    2.0
1    5.5
2    7.5
Name: a, dtype: float64

#1


You can use df.<column>.map to apply a function to each element in a column:

您可以使用df。 .map将函数应用于列中的每个元素:

df = pd.DataFrame({'a': 
    [np.array([1, 2, 3]), 
     np.array([4, 5, 6, 7]), 
     np.array([7, 8])]
})

df
Out[8]: 
              a
0     [1, 2, 3]
1  [4, 5, 6, 7]
2        [7, 8]

df['a'].map(lambda x: x.mean())
Out[9]: 
0    2.0
1    5.5
2    7.5
Name: a, dtype: float64