python读取数组的前10个数据_Python:获取数据框中多个数组的按元素均值

这篇博客介绍如何在Python中对含有1x35000数组的Pandas DataFrame进行操作,计算每列数组的按元素均值,生成一个新的1x10 DataFrame。示例展示了使用stack、groupby和apply函数实现这一目标,避免了使用for循环。
摘要由CSDN通过智能技术生成

I have a 16x10 panda dataframe with 1x35000 arrays (or NaN) in each cell. I want to take the element-wise mean over rows for each column.

1 2 3 ... 10

1 1x35000 1x35000 1x35000 1x35000

2 1x35000 NaN 1x35000 1x35000

3 1x35000 NaN 1x35000 NaN

...

16 1x35000 1x35000 NaN 1x35000

To avoid misunderstandings: take the first element of each array in the first column and take the mean. Then take the second element of each array in the first column and take the mean again. In the end I want to have a 1x10 dataframe with one 1x35000 array each per column. The array should be the element-wise mean of my former arrays.

1 2 3 ... 10

1 1x35000 1x35000 1x35000 1x35000

Do you have an idea to get there elegantly preferably without for-loops?

解决方案

Setup

np.random.seed([3,14159])

df = pd.DataFrame(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值