pandas - DataFrame 基础计算(求和、平均值、最大值、最小值、中位数、众数、方差、标准差、分位数)

pandas - DataFrame

1、 DataFrame计算


def genericCalFun():
    """
    DataFrame计算
    :return:
    """
    data = [
        [1, 1, 1, 1],
        [2, 1, 2, 1],
        [3, 1, 3, 1],
        [4, 1, 4, 1],
        [5, 1, 5, 1]
    ]
    columns = ['col-1', 'col-2', 'col-3', 'col-4']
    index = ['idx-1', 'idx-2', 'idx-3', 'idx-4', 'idx-5']

    df = pd.DataFrame(data=data, index=index, columns=columns)

    # 行求和
    row_sum = df.sum(axis=1)

    # 列求和
    row_col = df.sum(axis=0)

    # 平均值
    mean = df.mean()

    # 最大值
    max = df.max()

    # 最小
    min = df.min()

    # 中位数
    median = df.median()

    # 众数
    mode = df.mode()

    # 方差
    var = df.var(axis=1)

    # 标准差
    std = df.std(axis=1)

    # 分位数也称分位点,它以概率为依据将数据分割为几个等份(中位数(即二分位数)、四分位数)
    quantile = df.quantile(0.35)

原始数据

	       col-1  col-2  col-3  col-4
	idx-1      1      1      1      1
	idx-2      2      1      2      1
	idx-3      3      1      3      1
	idx-4      4      1      4      1
	idx-5      5      1      5      1

row_sum 行求和 返回结果:

	idx-1     4
	idx-2     6
	idx-3     8
	idx-4    10
	idx-5    12
	dtype: int64

row_col 列求和 返回结果:

	col-1    15
	col-2     5
	col-3    15
	col-4     5
	dtype: int64

mean 平均值 返回结果:

	col-1    3.0
	col-2    1.0
	col-3    3.0
	col-4    1.0
	dtype: float64

max 最大值 返回结果:

	col-1    5
	col-2    1
	col-3    5
	col-4    1
	dtype: int64

min 最小值 返回结果:

	col-1    1
	col-2    1
	col-3    1
	col-4    1
	dtype: int64

median 中位数 返回结果:

	col-1    3.0
	col-2    1.0
	col-3    3.0
	col-4    1.0
	dtype: float64

mode 众数 返回结果:

	   col-1  col-2  col-3  col-4
	0      1    1.0      1    1.0
	1      2    NaN      2    NaN
	2      3    NaN      3    NaN
	3      4    NaN      4    NaN
	4      5    NaN      5    NaN

var 方差 返回结果:

	idx-1    0.000000
	idx-2    0.333333
	idx-3    1.333333
	idx-4    3.000000
	idx-5    5.333333
	dtype: float64

std 标准差 返回结果:

	idx-1    0.000000
	idx-2    0.577350
	idx-3    1.154701
	idx-4    1.732051
	idx-5    2.309401
	dtype: float64

quantile 分位数 返回结果:

	col-1    2.4
	col-2    1.0
	col-3    2.4
	col-4    1.0
	Name: 0.35, dtype: float64

2、 shift偏移计算


def diffFun():
    """
    shift偏移计算
    :return:
    """
    data = [
        [10, 12],
        [12, 22],
        [17, 32],
        [15, 42],
        [20, 52]
    ]
    columns = ['col-1', 'col-2']
    index = ['idx-1', 'idx-2', 'idx-3', 'idx-4', 'idx-5']

    df = pd.DataFrame(data=data, index=index, columns=columns)

    df['差分'] = df['col-1'] - df['col-1'].shift()
    print(df)

col-1 利用 shift偏移计算

	       col-1  col-2   差分
	idx-1     10     12  NaN
	idx-2     12     22  2.0
	idx-3     17     32  5.0
	idx-4     15     42 -2.0
	idx-5     20     52  5.0
  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值