Reputation: 603
I have a column in a pandas data frame called 'X' that holds an array of numbers;
pd.head()
Row_ID X
1 [10,20]
2 [13]
3 [30,40]
I would like to take the mean of each array per row and replace it with column 'X';
Row_ID X
1 [15]
2 [13]
3 [35]
How can I do this in Python?
Upvotes: 1
Views: 1861
Reputation: 109756
Assuming you want a scalar value instead of a list or array, you could do the following:
df['X'] = [np.mean(x) for x in df.X]
>>> df
df
X
0 15
1 13
2 35
Upvotes: 2
Reputation: 7179
df['X'] = df.apply(lambda row: [[pd.np.mean(row['X'])]], axis=1)
>>> df
X
0 [15.0]
1 [13.0]
2 [35.0]
Upvotes: 1