Home > OS >  pandas iterate through rows based on multiple conditions and then subtract from column?
pandas iterate through rows based on multiple conditions and then subtract from column?

Time:05-30

I have a df looking something like this but larger:

df = pd.DataFrame({
    'Time' : [1,2,7,10,15,16,77,98,999,1000,1121,1245,1373,1490,1555],  
    'ID' : ['1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3'],
    'Act' : ['1', '2', '4', '4', '2', '0', '2', '4', '4', '1', '4', '4', '1', '1', '2'],
    'mean_bout_count' : ['2.3', '4', '7', '7', '1', '2', '2.2', '2.1', '2.1', '10', '3', '3', '3', '3', '3']})

For each row that is "Act_cat" == 4 and "mean_bout_count" < 3 I would like to take -1 from the "Act_cat" column. The code below takes a -1 from all rows as far as I can tell and also takes too long...

df = df.reset_index()  
for i, row in df.iterrows():
    if df.iloc[i]["Act_cat"] == 4 and df.iloc[i]["mean_bout_count"] < 3:
        df["Act_cat"] = df["Act_cat"]-1
    else: 
        df["Act_cat"] = df["Act_cat"]-0

Please let me know if you have a better idea!

Thank you!

CodePudding user response:

You could just do:

df.loc[df['Act'].eq(4) & df['mean_bout_count'].lt(3), 'Act'] -= 1
    Time ID  Act  mean_bout_count
0      1  1    1              2.3
1      2  1    2              4.0
2      7  1    4              7.0
3     10  1    4              7.0
4     15  1    2              1.0
5     16  2    0              2.0
6     77  2    2              2.2
7     98  2    3              2.1
8    999  2    3              2.1
9   1000  2    1             10.0
10  1121  3    4              3.0
11  1245  3    4              3.0
12  1373  3    1              3.0
13  1490  3    1              3.0
14  1555  3    2              3.0

The problem with your approach was that by doing df["Act_cat"] = df["Act_cat"]-1 within the if, you were subtracting 1 from the full column each time the condition evaluated to true.

  • Related