Reputation: 542
I am trying to slice my dataframe based on a certain condition, and select the first row of that slice, and set the value of the column of that first row.
index COL_A. COL.B. COLC
0. cond_A1 cond_B1. 0
1 cond_A1 cond_B1. 0
2 cond_A1 cond_B1. 0
3 cond_A2 cond_B2. 0
4. cond_A2 cond_B2. 0
Neither of the following lines of code I have attempted update the dataframe
df.loc[((df['COL_A'] == cond_A1) & (df['COL_B'] == cond_b1)), 'COL_C'].iloc[0] = 1
df[((df['COL_A'] == cond_A1) & (df['COL_B'] == cond_b1))].iloc[0]['COL_C'] = 1
I need to be able to loop through the conditions so that I could apply the same code to the next set of conditions, and update the COL_C row with index 3 based on these new conditions.
Upvotes: 0
Views: 34
Reputation: 10624
You can update only the first row of your slice with the following code:
df.loc[df.loc[(df['COL_A'] == cond_A1) & (df['COL_B'] == cond_b1), 'COL_C'].index[0], 'COL_C'] = 1
Upvotes: 1