Home > Mobile >  Combining looping and conditional to make new columns on dataframe
Combining looping and conditional to make new columns on dataframe

Time:11-08

I want to make a function with loop and conditional, that count only when Actual Result = 1. So the numbers always increase by 1 if the Actual Result = 1.

This is my dataframe:

enter image description here

This is my code but it doesnt produce the result that i want :

def func_count(x):
for i in range(1,880):
    if x['Actual Result']==1:
        result = i
    else:
        result = '-'
    return result

X_machine_learning['Count'] = X_machine_learning.apply(lambda x:func_count(x),axis=1)

When i check & filter with count != '-' The result will be like this :

enter image description here

The number always equal to 1 and not increase by 1 everytime the actual result = 1. Any solution?

CodePudding user response:

Try something like this:

import pandas as pd

df = pd.DataFrame({
    'age': [30,25,40,12,16,17,14,50,22,10],
    'actual_result': [0,1,1,1,0,0,1,1,1,0]
})

count = 0
lst_count = []
for i in range(len(df)):
    if df['actual_result'][i] == 1:
        count =1
        lst_count.append(count)
    else:
        lst_count.append('-')

df['count'] = lst_count
print(df)

Result

   age  actual_result count
0   30              0     -
1   25              1     1
2   40              1     2
3   12              1     3
4   16              0     -
5   17              0     -
6   14              1     4
7   50              1     5
8   22              1     6
9   10              0     -

CodePudding user response:

Actually, you don't need to loop over the dataframe, which is mostly a Pandas-antipattern that should be avoided. With df your dataframe you could try the following instead:

m = df["Actual Result"] == 1
df["Count"] = m.cumsum().where(m, "-")

Result for the following dataframe

df = pd.DataFrame({"Actual Result": [1, 1, 0, 1, 1, 1, 0, 0, 1, 0]})
   Actual Result
0              1
1              1
2              0
3              1
4              1
5              1
6              0
7              0
8              1
9              0

is

   Actual Result Count
0              1     1
1              1     2
2              0     -
3              1     3
4              1     4
5              1     5
6              0     -
7              0     -
8              1     6
9              0     -
  • Related