【问题标题】:python function on pandas df using multiple columns and reset variablepandas df上的python函数使用多列和重置变量
【发布时间】:2017-05-19 23:20:35
【问题描述】:

请问在 python/pandas 中执行以下操作的最佳方法是什么?

我想计算趋势数据 2 与趋势数据 1 不一致的发生次数,并在每次趋势数据 1 更改时重置计数器。

在此示例中,我正在努力寻找在数据框中创建新列 df['D'] 的正确方法。

df['A'] = 趋势数据 1
df['B'] = 布尔指标,如果趋势数据 1 发生变化
df['C'] = 趋势数据 2
df['D'] = 期望结果

df['A']        df['B']        df['C']        df['D']            
  1                0              1            0
  1                0              1            0
 -1                1             -1            0
 -1                0             -1            0
 -1                0              1            1
 -1                0             -1            1
 -1                0             -1            1
 -1                0              1            2 
 -1                0              1            2
 -1                0             -1            2
  1                1              1            0
  1                0              1            0
  1                0             -1            1
  1                0              1            1
  1                0             -1            2
  1                0              1            2
  1                0              1            2

在excel中我会简单地使用:

=IF(B2=1,0,IF(AND((C2<>C1),(C2<>A2)),D1+1,D1))

但是,我一直在为无法引用 pandas 中的先前单元格而苦苦挣扎。

我不能使用np.where()。我确信它只是以正确的方式应用一个函数,但我似乎无法让它引用其他列并重置变量。我查看了其他答案,但在这种情况下似乎找不到任何工作。

类似

  • 注意:创建df['E'] = df['C'].shift(1)

def corrections(x):

    if df['B'] == 1:    
        x = 0
    elif ((df['C'] != df['E']) AND ( df['C'] != df['A'])):
        x = x + 1
    else:
        x

抱歉,因为我觉得我在这个问题上遗漏了一些相当简单的东西,但只是继续绕圈子!

【问题讨论】:

  • 您能否编辑您的问题以解释第一组 2 的来源?

标签: python function pandas lambda


【解决方案1】:
def make_D (df):

    counter = 0

    array = []

    for index in df.index:

        if df.loc[index, 'A']!=df.loc[index, 'C']:

           counter = counter + 1

        if index>0:

            if df.loc[index, 'B'] != df.loc[index-1, 'B']:

                 counter = 0

        array.append(counter)

    df['D'] = array

    return (df)

new_df = make_D(df)

希望对你有帮助!

【讨论】:

    【解决方案2】:
    #Set a list to store values for column D
    d = []
    
    #calculate D using the given conditions
    df.apply(lambda x: d.append(0) if ((x.name==0)|(x.B==1)) else d.append(d[-1]+1) if (x.C!=df.iloc[x.name-1].C) & (x.C!=x.A) else d.append(d[-1]), axis=1)
    
    #set columns D using values from the list d.
    df['D'] = d
    
    Out[594]: 
        A  B  C  D
    0   1  0  1  0
    1   1  0  1  0
    2  -1  1 -1  0
    3  -1  0 -1  0
    4  -1  0  1  1
    5  -1  0 -1  1
    6  -1  0 -1  1
    7  -1  0  1  2
    8  -1  0  1  2
    9  -1  0 -1  2
    10  1  1  1  0
    11  1  0  1  0
    12  1  0 -1  1
    13  1  0  1  1
    14  1  0 -1  2
    15  1  0  1  2
    16  1  0  1  2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2018-07-06
      • 2017-02-24
      • 2017-05-10
      • 2023-01-14
      • 2018-05-01
      • 1970-01-01
      相关资源
      最近更新 更多