相关疑难解决方法(0)

熊猫中的轴是什么意思?

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

import pandas as pd
import numpy as np

dff = pd.DataFrame(np.random.randn(1,2),columns=list('AB'))
Run Code Online (Sandbox Code Playgroud)

然后我得到了数据帧:

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      0     | 0.626386| 1.52325|
+------------+---------+--------+
Run Code Online (Sandbox Code Playgroud)

当我输入命令时:

dff.mean(axis=1)
Run Code Online (Sandbox Code Playgroud)

我有 :

0    1.074821
dtype: float64
Run Code Online (Sandbox Code Playgroud)

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

A    0.626386
B    1.523255
dtype: float64
Run Code Online (Sandbox Code Playgroud)

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

python numpy dataframe pandas

243
推荐指数
12
解决办法
19万
查看次数

Pandas Dataframe/Numpy Array"轴"定义中的歧义

关于如何定义python轴以及它们是否引用DataFrame的行或列,我一直很困惑.请考虑以下代码:

>>> df = pd.DataFrame([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], columns=["col1", "col2", "col3", "col4"])
>>> df
   col1  col2  col3  col4
0     1     1     1     1
1     2     2     2     2
2     3     3     3     3
Run Code Online (Sandbox Code Playgroud)

因此,如果我们打电话df.mean(axis=1),我们将获得跨行的平均值:

>>> df.mean(axis=1)
0    1
1    2
2    3
Run Code Online (Sandbox Code Playgroud)

但是,如果我们调用df.drop(name, axis=1),我们实际上会删除一列而不是一行:

>>> df.drop("col4", axis=1)
   col1  col2  col3
0     1     1     1
1     2     2     2
2     3     3     3
Run Code Online (Sandbox Code Playgroud)

有人能帮助我理解pandas/numpy/scipy中"轴"的含义吗?

旁注,DataFrame.mean可能被定义错了.它在文档中说, …

python arrays numpy dataframe pandas

81
推荐指数
4
解决办法
2万
查看次数

标签 统计

dataframe ×2

numpy ×2

pandas ×2

python ×2

arrays ×1