简体   繁体   中英

check on element in a row in a data frame in pandas is NaN then replace it

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM