pandas.DF()中的列是否单调增加? [英] Is a column in pandas.DF() monotonically increasing?

查看:236
本文介绍了pandas.DF()中的列是否单调增加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用is_monotonic方法检查pandas.DataFrame()的索引是否单调增加.但是,我想检查列值之一是否严格增加value(float/integer)?

I can check if the index of a pandas.DataFrame() is monotonically increasing by using is_monotonic method. However, I would like to check if one of the column value is strictly increasing in value(float/integer) ?

In [13]: my_df = pd.DataFrame([1,2,3,5,7,6,9])

In [14]: my_df
Out[14]: 
   0
0  1
1  2
2  3
3  5
4  7
5  6
6  9

In [15]: my_df.index.is_monotonic
Out[15]: True

推荐答案

Pandas 0.19 添加了公共

Pandas 0.19 added a public Series.is_monotonic API (previously, this was available only in the undocumented algos module).

(已更新)请注意,尽管名称为Series.is_monotonic,但它仅表示序列是否单调递增(等同于使用Series.is_monotonic_increasing).对于另一种方法,请使用Series.is_monotonic_decreasing. 无论如何,两者都不是严格的,但是您可以将它们与 is_unqiue 以获得严格要求.

(Updated) Note that despite its name, Series.is_monotonic only indicates whether a series is monotonically increasing (equivalent to using Series.is_monotonic_increasing). For the other way around, use Series.is_monotonic_decreasing. Anyway, both are non-strict, but you can combine them with is_unqiue to get strictness.

例如:

my_df = pd.DataFrame([1,2,2,3], columns = ['A'])

my_df['A'].is_monotonic    # non-strict
Out[1]: True

my_df['A'].is_monotonic_increasing    # equivalent to is_monotonic
Out[2]: True

(my_df['A'].is_monotonic_increasing and my_df['A'].is_unique)    # strict  
Out[3]: False

my_df['A'].is_monotonic_decreasing    # Other direction (also non-strict)
Out[4]: False

您可以使用apply在DataFrame级别上运行它:

You can use apply to run this at a DataFrame level:

my_df = pd.DataFrame({'A':[1,2,3],'B':[1,1,1],'C':[3,2,1]})
my_df
Out[32]: 
   A  B  C
0  1  1  3
1  2  1  2
2  3  1  1

my_df.apply(lambda x: x.is_monotonic)
Out[33]: 
A     True
B     True
C    False
dtype: bool

这篇关于pandas.DF()中的列是否单调增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆