Pandas 中 axis 的含义
本教程解释了在 DataFrames 和 Series 等 Pandas 对象的各种方法中使用的 axis
参数的含义。
import pandas as pd
empl_df = pd.DataFrame(
{
"Name": ["Jon", "Willy", "Mike", "Luna", "Sam", "Aliza"],
"Age": [30, 33, 35, 30, 30, 31],
"Weight(KG)": [75, 75, 80, 70, 73, 70],
"Height(meters)": [1.7, 1.7, 1.85, 1.75, 1.8, 1.75],
"Salary($)": [3300, 3500, 4000, 3050, 3500, 3700],
}
)
print(empl_df)
输出:
Name Age Weight(KG) Height(meters) Salary($)
0 Jon 30 75 1.70 3300
1 Willy 33 75 1.70 3500
2 Mike 35 80 1.85 4000
3 Luna 30 70 1.75 3050
4 Sam 30 73 1.80 3500
5 Aliza 31 70 1.75 3700
我们使用 DataFrame empl_df
来解释如何在 Pandas 方法中使用 axis
参数。
在 Pandas 方法中使用 axis
参数
axis
参数指定在 DataFrame 中应用特定方法或函数的方向。axis=0
代表函数是列式应用,axis=1
表示函数是行式应用在 DataFrame 上。
如果我们按列应用函数,我们将得到一个单行的结果;如果按行应用函数,我们将得到一个单列的 DataFrame。
示例:在 Pandas 方法中使用 axis=0
import pandas as pd
empl_df = pd.DataFrame(
{
"Name": ["Jon", "Willy", "Mike", "Luna", "Sam", "Aliza"],
"Age": [30, 33, 35, 30, 30, 31],
"Weight(KG)": [75, 75, 80, 70, 73, 70],
"Height(meters)": [1.7, 1.7, 1.85, 1.75, 1.8, 1.75],
"Salary($)": [3300, 3500, 4000, 3050, 3500, 3700],
}
)
print("The Employee DataFrame is:")
print(empl_df, "\n")
print("The DataFrame with mean values of each column is:")
print(empl_df.mean(axis=0))
输出:
The Employee DataFrame is:
Name Age Weight(KG) Height(meters) Salary($)
0 Jon 30 75 1.70 3300
1 Willy 33 75 1.70 3500
2 Mike 35 80 1.85 4000
3 Luna 30 70 1.75 3050
4 Sam 30 73 1.80 3500
5 Aliza 31 70 1.75 3700
The DataFrame with mean values of each column is:
Age 31.500000
Weight(KG) 73.833333
Height(meters) 1.758333
Salary($) 3508.333333
dtype: float64
它计算 DataFrame empl_df
的按列平均值。平均值只计算有数值的列。
如果我们设置 axis=0
,它将通过对该列的行值进行平均来计算每列的平均值。
例子在 Pandas 方法中使用 axis=1
import pandas as pd
empl_df = pd.DataFrame(
{
"Name": ["Jon", "Willy", "Mike", "Luna", "Sam", "Aliza"],
"Age": [30, 33, 35, 30, 30, 31],
"Weight(KG)": [75, 75, 80, 70, 73, 70],
"Height(meters)": [1.7, 1.7, 1.85, 1.75, 1.8, 1.75],
"Salary($)": [3300, 3500, 4000, 3050, 3500, 3700],
}
)
print("The Employee DataFrame is:")
print(empl_df, "\n")
print("The DataFrame with mean values of each row is:")
print(empl_df.mean(axis=1))
输出:
The Employee DataFrame is:
Name Age Weight(KG) Height(meters) Salary($)
0 Jon 30 75 1.70 3300
1 Willy 33 75 1.70 3500
2 Mike 35 80 1.85 4000
3 Luna 30 70 1.75 3050
4 Sam 30 73 1.80 3500
5 Aliza 31 70 1.75 3700
The DataFrame with mean values of each row is:
0 851.6750
1 902.4250
2 1029.2125
3 787.9375
4 901.2000
5 950.6875
dtype: float64
它计算 DataFrame empl_df
的行平均值,换句话说,它将计算每行的平均值,通过对该行的数值类型的列值进行平均。我们将在最后得到一个单列的每行平均值。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串