下面是我生成一个数据框架的代码:
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
我的问题是:轴在熊猫中是什么意思?
轴在编程中是形状元组中的位置。这里有一个例子:
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,)。
我认为,正确答案应该是“这很复杂”。
“轴”这个词本身在不同的人心中会产生不同的形象
假设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
我认为还有另一种理解方式。
对于np。数组,如果我们想要消除列,我们使用axis = 1;如果我们想消除行,我们使用axis = 0。
np.mean(np.array(np.ones(shape=(3,5,10))),axis = 0).shape # (5,10)
np.mean(np.array(np.ones(shape=(3,5,10))),axis = 1).shape # (3,10)
np.mean(np.array(np.ones(shape=(3,5,10))),axis = (0,1)).shape # (10,)
对于pandas对象,axis = 0表示按行操作,axis = 1表示按列操作。这与numpy的定义不同,我们可以检查numpy.doc和pandas.doc的定义
让我们看看Wiki上的表格。这是国际货币基金组织对2010年至2019年前十大国家GDP的估计。
1. 如果你想计算每个国家过去十年(2010-2019)的平均GDP,你需要做,df.mean(轴=1)。例如,如果你想计算美国从2010年到2019年的平均GDP, df。loc['美国',' 2010 ':' 2019 '].mean(轴= 1)
2. 如果我想计算所有国家每年的平均GDP(平均值),你需要做,df.mean(轴=0)。例如,如果你想计算2015年美国、中国、日本、德国和印度的平均GDP, df。loc(“美国”:“印度”,' 2015 '].mean(轴= 0)
注意:上述代码只有在使用set_index方法将“国家(或附属领土)”列设置为索引后才能工作。
在过去的一个小时里,我也一直在试着求出坐标轴。上述所有答案中的语言,以及文档都没有任何帮助。
要回答我现在理解的问题,在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时,您将修复行标题并更改数据,这些数据将来自不同的列。