pandas 中 axis 的含义-ag捕鱼王app官网

pandas 中 axis 的含义

作者:迹忆客 最近更新:2024/04/23 浏览次数:

本教程解释了在 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 由两列来 groupby

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图