下面是我生成一个数据框架的代码:
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代表列,我希望命令的结果是
A 0.626386
B 1.523255
dtype: float64
我的问题是:轴在熊猫中是什么意思?
我认为,正确答案应该是“这很复杂”。
“轴”这个词本身在不同的人心中会产生不同的形象
假设y轴,它应该是一个垂直的图像。但是,现在考虑一条垂直线x=0。这也是一条垂直线,但是x轴上的值为0。
类似地,当我们说axis='index'(意思是axis=0)时,我们说的是索引所在的“垂直”方向吗?或者由索引值处理的一系列数据?熊猫往往意味着第一个意思,垂直方向。
熊猫本身也不是100%一致的,看看下面的例子,它们几乎有相同的共同主题:
# [1] piling dfs
pd.concat([df0, df1], axis='index')
# adding dfs on top of each other vertically like pilling up a column,
# but, we will use the word 'index'
# [2] for every column in df: operate on it
df.apply(foo, axis='index')
df.mean('A', axis='index')
a_boolean_df.all(axis='index')
# apply an operation to a vertical slice of data, ie. a column,
# then apply the same operation to the next column on the right
# then to the right again... until the last column
# but, we will use the word 'index'
# [3] delete a column or row of data
df.drop(axis='index', ...)
df.dropna(axis='index', ...)
# this time, we are droping an index/row, a horizontal slice of data.
# so OBVIOUSLY we will use the word 'index'
# [4] drop duplicate
df.drop_duplicates(subset=['mycolumn0', 'mycolumn1']...)
# thank God we don't need to deal with the "axis" bs in this
轴在编程中是形状元组中的位置。这里有一个例子:
import numpy as np
a=np.arange(120).reshape(2,3,4,5)
a.shape
Out[3]: (2, 3, 4, 5)
np.sum(a,axis=0).shape
Out[4]: (3, 4, 5)
np.sum(a,axis=1).shape
Out[5]: (2, 4, 5)
np.sum(a,axis=2).shape
Out[6]: (2, 3, 5)
np.sum(a,axis=3).shape
Out[7]: (2, 3, 4)
轴上的均值将导致该维度被移除。
参考原题,dff形状为(1,2)。使用axis=1将形状更改为(1,)。
正确使用axis=的问题在于它在两种主要不同情况下的使用:
用于计算累积值或重新排列(如排序)数据。
用于操纵(“玩”)实体(例如数据帧)。
这个答案背后的主要思想是为了避免混淆,我们选择一个数字或一个名称来指定特定的轴,以更清楚、直观和描述性的为准。
Pandas基于NumPy, NumPy基于数学,特别是n维矩阵。下面是三维空间中数学中常用的轴的名称:
这张图仅用于记忆坐标轴的序数:
x轴为0,
y轴为1,和
z轴为2。
z轴仅用于面板;对于数据框架,我们将把我们的兴趣限制在带有x轴(0,垂直)和y轴(1,水平)的绿色二维基本平面上。
这都是关于axis= parameter的潜在值的数字。
轴的名称是“索引”(你可以使用别名“行”)和“列”,为了解释这些名称和(轴的)序数之间的关系并不重要,因为每个人都知道“行”和“列”是什么意思(这里的每个人-我想-都知道“索引”在pandas中的意思)。
现在,我的建议是
If you want to compute an accumulated value, you may compute it from values located along axis 0 (or along axis 1) — use axis=0 (or axis=1).
Similarly, if you want to rearrange values, use the axis number of the axis, along which are located data for rearranging (e.g. for sorting).
If you want to manipulate (e.g. concatenate) entities (e.g. dataframes) — use axis='index' (synonym: axis='rows') or axis='columns' to specify the resulting change — index (rows) or columns, respectively.
(For concatenating, you will obtain either a longer index (= more rows), or more columns, respectively.)
在过去的一个小时里,我也一直在试着求出坐标轴。上述所有答案中的语言,以及文档都没有任何帮助。
要回答我现在理解的问题,在Pandas中,axis = 1或0意味着在应用函数时希望保持哪个轴头不变。
注意:当我说标题时,我指的是索引名
扩展你的例子:
+------------+---------+--------+
| | A | B |
+------------+---------+---------
| X | 0.626386| 1.52325|
+------------+---------+--------+
| Y | 0.626386| 1.52325|
+------------+---------+--------+
对于axis=1=columns:我们保持列标题不变,并通过改变数据应用平均值函数。
为了演示,我们保持列标题为常量:
+------------+---------+--------+
| | A | B |
现在我们填充A和B值的一个集合,然后找到平均值
| | 0.626386| 1.52325|
然后我们填充下一组A和B值,并找到平均值
| | 0.626386| 1.52325|
类似地,对于axis=rows,我们保持行标题不变,并不断更改数据:
为了演示,首先修复行标题:
+------------+
| X |
+------------+
| Y |
+------------+
现在填充第一组X和Y值,然后求平均值
+------------+---------+
| X | 0.626386
+------------+---------+
| Y | 0.626386
+------------+---------+
然后填充下一组X和Y值,然后找到平均值:
+------------+---------+
| X | 1.52325 |
+------------+---------+
| Y | 1.52325 |
+------------+---------+
总之,
当axis=columns时,将修复列标题并更改数据,这些数据将来自不同的行。
当axis=rows时,您将修复行标题并更改数据,这些数据将来自不同的列。