Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas提供了大量能使我们快速便捷地处理数据的函数和方法。你很快就会发现,它是使Python成为强大而高效的数据分析环境的重要因素之一。本文主要介绍一下Pandas中pandas.DataFrame.cummax方法的使用。

DataFrame.cummax(axis=None, skipna=True, *args, **kwargs)

[source]

返回一个DataFrame或Series轴上的累积最大值。

返回包含累积最大值的相同大小的DataFrame或Series。

参数:

axis : {0 或 ‘index’, 1 或 ‘columns’}, 默认 0

索引或轴的名称。0等于None或' index '。

skipna : boolean, 默认 True

排除NA/null值。如果整个行/列是NA,那么结果将是NA。

*args, **kwargs :

附加关键字没有效果,但是可以接受与NumPy兼容。

返回:

cummax : Series 或 DataFrame

例子

Series

>>> s = pd.Series([2, np.nan, 5, -1, 0])
>>> s
0 2.0
1 NaN
2 5.0
3 -1.0
4 0.0
dtype: float64

默认情况下,NA值被忽略

>>> s.cummax()
0 2.0
1 NaN
2 5.0
3 5.0
4 5.0
dtype: float64

若要在操作中包含NA值,请使用skipna=False

>>> s.cummax(skipna=False)
0 2.0
1 NaN
2 NaN
3 NaN
4 NaN
dtype: float64

DataFrame

>>> df = pd.DataFrame([[2.0, 1.0],
... [3.0, np.nan],
... [1.0, 0.0]],
... columns=list('AB'))
>>> df
A B
0 2.0 1.0
1 3.0 NaN
2 1.0 0.0

认情况下,遍历行并在每一列中找到最大值。这相当于axis=None或axis='index'

>>> df.cummax()
A B
0 2.0 1.0
1 3.0 NaN
2 3.0 1.0

若要遍历列并找到每行中的最大值,请使用axis=1

>>> df.cummax(axis=1)
A B
0 2.0 2.0
1 3.0 NaN
2 1.0 1.0

推荐文档

相关文档

大家感兴趣的内容

随机列表