如何根据布尔条件更改 pandas 数据框中的单元格

How to change a cell in pandas dataframe according to boolean condition

我必须关注数据帧。 我成功地从这个数据框的闰年中删除了 2 月 29 日的所有天数,因为我打算按“一年中的第几天”列(使用 .dt.dayofyear 创建)进行分组,并且我决定忽略额外的闰年一天。 现在,为了按“一年中的第几天”列分组,如果这一天是三月一日或更晚,我必须从闰年的天数中减去 1。否则,闰年将有 366 天而不是 355 天(即使删除闰日)。

这是我的代码:

clim_rec = pd.read_csv("daily_climate_records.csv")
clim_rec['Date'] = pd.to_datetime(clim_rec['Date']) # converting "Date" column from string into datetime format

# Let's drop all leaping days by masking all Feb 29 days
feb_29_mask = ~((clim_rec.Date.dt.month == 2) & (clim_rec.Date.dt.day == 29))
clim_rec = clim_rec.where(feb_29_mask).dropna()

# Let's add new column with the "day of year" in order to group by this column
clim_rec['Day of year'] = clim_rec['Date'].dt.dayofyear
print(clim_rec.head())
#print('---------------------------------------------------')
# Now, if the year is a leap year and the dayofyear is greater than the dayofyear of Feb-29
# we subtract 1 from dayofyear. After doing that we will get values 1-365 for dayofyear
leap_year_mask = (clim_rec.Date.dt.year % 4 == 0) & ((clim_rec.Date.dt.year % 100 != 0)
                 |(clim_rec.Date.dt.year % 400 == 0)) & (clim_rec.Date.dt.month >=3)

clim_rec['Day of year'] = clim_rec['Day of year'].apply(lambda x: x-1) # this line is not correct

我的问题是: 如何修改我附加代码的最后一行,以便仅对符合布尔掩码条件

的特定行应用减法

使用 DataFrame.loc 作为 select 行的掩码,更好/更快的减去 1 而不是 apply 以避免循环(因为在引擎盖下应用使用循环) :

clim_rec.loc[leap_year_mask, 'Day of year'] -= 1 

工作方式:

clim_rec.loc[leap_year_mask, 'Day of year'] = clim_rec.loc[leap_year_mask, 'Day of year']-1

这对你有用吗?氪

clim_rec['mask'] = leaf_year_mask
clim_rec['Day of year'] =  clim_rec.apply(lambda x: x['Day of year']-1 if x['mask'] else x['Day of year'])