pandas 中的轴是什么意思? [英] What does axis in pandas mean?

查看:14
本文介绍了 pandas 中的轴是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我生成数据帧的代码:

Here is my code to generate a dataframe:

import pandas as pd
import numpy as np

dff = pd.DataFrame(np.random.randn(1,2),columns=list('AB'))

然后我得到了数据框:

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      0     | 0.626386| 1.52325|
+------------+---------+--------+

当我输入命令时:

dff.mean(axis=1)

我得到了:

0    1.074821
dtype: float64

根据pandas的引用,axis=1代表列,我希望命令的结果是

According to the reference of pandas, axis=1 stands for columns and I expect the result of the command to be

A    0.626386
B    1.523255
dtype: float64

所以这是我的问题:pandas 中的轴是什么意思?

So here is my question: what does axis in pandas mean?

推荐答案

它指定了计算均值的.默认axis=0.这与 axis 指定显式时的 numpy.mean 用法一致(在 numpy.mean 中,axis==None 默认情况下,它计算扁平数组上的平均值),其中 axis=0 沿着 rows(即,index inpandas) 和 axis=1 沿 .为了更加清晰,可以选择指定 axis='index'(而不是 axis=0)或 axis='columns'(而不是axis=1).

It specifies the axis along which the means are computed. By default axis=0. This is consistent with the numpy.mean usage when axis is specified explicitly (in numpy.mean, axis==None by default, which computes the mean value over the flattened array) , in which axis=0 along the rows (namely, index in pandas), and axis=1 along the columns. For added clarity, one may choose to specify axis='index' (instead of axis=0) or axis='columns' (instead of axis=1).

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      0     | 0.626386| 1.52325|----axis=1----->
+------------+---------+--------+
             |         |
             | axis=0  |
             ↓         ↓

这篇关于 pandas 中的轴是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆