简体   繁体   中英

pandas: calculate mean of numpy array for each row in a column

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

   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, ie row, for the entire column? Something like :

df.A.mean()

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM