2

part of my data frame is as following:

personId    ActivityType    Time         Act_delay
1473237100  remote_work_4   57651.0      57651.0    
1473237100  home_2          59185.0      59185.0    
1473237100  remote_work_5   65849.0      65849.0    
1473237100  home_1          NaN          0.0    

and I want to check if, in any row, the "ActivityType" column is equal to "home_1" and if "Time" column is NaN then replace "Act_delay" column to 10800. I have the following code"


for i, row in df.iterrows():
            if row['ActivityType'] == "home_1":
                if  row['Time'] == np.object:
                    df.loc[i,'Act_delay'] = 10800.0

but it does not work. the result is the same as before. what should I do?

sahel
  • 201
  • 2
  • 8

1 Answers1

3

Looping in a dataframe is not recommended, instead we can leverage bitwise & to combine the conditions (check variable mask) , then use df.loc[] for boolean indexing and selecting the desired series to assign the values.

mask = df['ActivityType'].eq('home_1') & df['Time'].isna()

df.loc[mask, 'Act_delay'] = 10800

Output:

     personId   ActivityType     Time  Act_delay
0  1473237100  remote_work_4  57651.0    57651.0
1  1473237100         home_2  59185.0    59185.0
2  1473237100  remote_work_5  65849.0    65849.0
3  1473237100         home_1      NaN    10800.0
anky
  • 74,114
  • 11
  • 41
  • 70
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74