简体   繁体   English

如何根据 boolean 条件更改 pandas dataframe 中的单元格

[英]How to change a cell in pandas dataframe according to boolean condition

I have to following dataframe我必须关注 dataframe 气候记录 . . I successfuly deleted all Feb 29 days from leaping years this dataframe because I intend to groupby "Day of year" column (which was create using.dt.dayofyear) and I decided to ignore the extra day of leaping years.我成功地删除了这个 dataframe 闰年的所有 2 月 29 天,因为我打算按“一年中的一天”列(使用.dt.dayofyear 创建)分组,我决定忽略闰年的额外一天。 Now, in order to group by "Day of year" column, i have to subsract 1 from days of leaping years if the day is first of March or later.现在,为了按“一年中的一天”列进行分组,如果这一天是三月的第一天或更晚,我必须从闰年的天数中减去 1。 otherwise, the leaping years will have 366 days instead of 355 (even after deleting the leaping days).否则,闰年将有 366 天而不是 355 天(即使在删除闰日之后)。

Here is my code:这是我的代码:

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

My question is: How to modify the last line of my attached code in order to apply the substraction only for the specific rows that are true accirding the boolean mask condition我的问题是:如何修改附加代码的最后一行,以便仅对根据 boolean 掩码条件为真的特定行应用减法

Use DataFrame.loc for select rows by mask, better/ faster is subtract by 1 instead apply for avoid loops (because apply under the hood use loops):通过掩码将DataFrame.loc用于 select 行,更好/更快地减去1而不是apply避免循环(因为在引擎盖下应用循环):

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

working like:像这样工作:

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

Would this work for you?这对你有用吗? Kr.氪。

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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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