簡體   English   中英

如何根據 boolean 條件更改 pandas dataframe 中的單元格

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

我必須關注 dataframe 氣候記錄 . 我成功地刪除了這個 dataframe 閏年的所有 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

我的問題是:如何修改附加代碼的最后一行,以便僅對根據 boolean 掩碼條件為真的特定行應用減法

通過掩碼將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'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM