【问题标题】:How can you find when a value changes throughout every row in a data frame?您如何找到数据框中每一行的值何时发生变化?
【发布时间】:2019-08-14 01:01:45
【问题描述】:

我正在尝试将帐户标记为新帐户、当前帐户、丢失帐户或返回帐户,但我遇到了逻辑问题。行索引是帐户,列是年份,值是 1 和 0,表示帐户是否处于活动状态。这是我到目前为止想出的。我不确定这是否会奏效,或者我是否已经接近,我不确定逻辑将如何寻找回头客。 df2 是原始数据框,df3 = df2.shift(periods=1,axis=1)

def differences():
    if df2 != df3 & df2 == 1:
        return "New"
    elif df2 != df3 & df2 ==0:
        return "Lost"
    elif df2 == df3 & df2 ==0:
        return ""
    else:
        return "Continuing"
differences() 

`

当我运行此代码时,我收到以下错误:

couldn't find matching opcode for 'and_bdl'

【问题讨论】:

    标签: python-3.x pandas jupyter-notebook


    【解决方案1】:

    以下代码逻辑可能适用于您的情况。

    编辑:根据您的评论,我修改了代码,以便检查除最后​​一列之外的所有列。

    import pandas as pd
    
    str="""account  2019  2018  2017  2016  2015
    alex 1 0 0 0 0
    joe  0 0 1 0 0
    boss  1 1 1 1 1
    smith 1 1 0 1 0"""
    df = pd.read_csv(pd.io.common.StringIO(str), sep='\s+', index_col='account')
    df
    #Out[46]: 
    #         2019  2018  2017  2016  2015
    #account                              
    #alex        1     0     0     0     0
    #joe         0     0     1     0     0
    #boss        1     1     1     1     1
    #smith       1     1     0     1     0
    
    # find account status per-year
    def account_status(x):
        status = []
        n = x.size
        for i in range(n-1):
            if x.iloc[i] == 1: 
                # if all rest are '0'
                if x.iloc[i+1:].eq(0).all():
                    status.extend(['new'] + [None]*(n-i-2))
                    break            
                # if the previous year is '0'
                elif x.iloc[i+1] == 0:
                    status.append('returning')
                else:
                    status.append('continuing')
            else:
                # at least one '1' in previous years
                if x.iloc[i+1:].eq(1).any():
                    status.append('lost')
                else:
                    status.extend([None] * (n-i-1))
                    break
        return status    
    
    s = df.apply(account_status, axis=1).apply(pd.Series)
    s.columns = df.columns[:-1]
    s                                                                                                                   
    #Out[57]: 
    #               2019        2018        2017        2016
    #account                                                
    #alex            new        None        None        None
    #joe            lost        lost         new        None
    #boss     continuing  continuing  continuing  continuing
    #smith    continuing   returning        lost         new
    

    【讨论】:

    • 非常感谢您的回复!这段代码很有意义。有没有办法对其进行调整以使其为每一列返回一个值?例如返回#smith继续返回丢失的新“”
    • 这正是我想要的结果。非常感谢您的努力、耐心和洞察力。我对 python 很陌生,所以你向我展示了很多我会深入研究的东西,因为我希望经常使用 pandas。
    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 2020-08-14
    • 2013-06-28
    • 1970-01-01
    • 2020-06-05
    相关资源
    最近更新 更多