简体   繁体   English

如何根据日期从 dataframe 中提取值?

[英]How to extract values from a dataframe based on dates?

I have a DataFrame (see image below) with daily values of which I want to retrieve a couple of values.我有一个 DataFrame(见下图),其中包含我想检索几个值的每日值。 I want to retrieve: Monthly mean, Monthly min, Monthly max, Yearly min, Amount of years etc. Currently I am resampling my DataFrame to retrieve these values (see code below) but there must be a better way.我想检索:每月平均值、每月最小值、每月最大值、每年最小值、年数等。目前我正在重新采样 DataFrame 以检索这些值(参见下面的代码),但必须有更好的方法。

数据框

 T_monthly=pd.DataFrame()                                                   # Create dataframe for monthly temperatures
 T_monthly['Basse']=temp_basse.iloc[:,3].resample("M").mean()               # Add Basse
 T_monthly['Basse_min']=temp_basse.iloc[:,3].resample("M").min()  
 T_monthly['Basse_max']=temp_basse.iloc[:,3].resample("M").max()  

 T_monthly['Kedougou']=temp_kedougou.iloc[:,3].resample("M").mean()         # Add Kedougou
 T_monthly['Kedougou_min']=temp_kedougou.iloc[:,3].resample("M").min()  
 T_monthly['Kedougou_max']=temp_kedougou.iloc[:,3].resample("M").max()  

 T_yearly=pd.DataFrame()                                                    # Create dataframe for yearly temperatures
 T_yearly['Basse_min']=temp_basse.iloc[:,3].resample("Y").min()  
 T_yearly['Basse_max']=temp_basse.iloc[:,3].resample("Y").max()
 T_yearly['Kedougou_min']=temp_kedougou.iloc[:,3].resample("Y").min()  
 T_yearly['Kedougou_max']=temp_kedougou.iloc[:,3].resample("Y").max() 

Preferably I want to have a function where I can put in a date, eg 2014, 2 (Y, M) and retrieve all the necessary values without resampling to new DataFrames.最好我想要一个 function ,我可以在其中输入一个日期,例如 2014, 2 (Y, M) 并检索所有必要的值,而无需重新采样到新的 DataFrames。 For example:例如:

input: (2014, 2)输入: (2014, 2)

output: [monthly mean, monthly min, monthly max, yearly min, yearly max] output:[月平均值、月最小值、月最大值、年最小值、年最大值]

At the moment my functions make use of the index number of the dataframes (see below).目前我的函数使用数据帧的索引号(见下文)。 However, if I want to put in a certain date I first need to find the corresponding index number.但是,如果我想输入某个日期,我首先需要找到相应的索引号。

 T_monthly.iloc[i, 3]

Thanks in advance!提前致谢!

Added two more columns after the sugestion of Sowjanya R Bhat, containing ['year'] and ['month']在 Sowjanya R Bhat 的建议之后又添加了两列,包含 ['year'] 和 ['month']

 df['year'] = pd.DatetimeIndex(df['date']).year
 df['month'] = pd.DatetimeIndex(df['date']).month

Next selecting data by:接下来选择数据:

 df.loc[(df['year'] == year) & (df['month'] == month)].iloc[:,3]

Not exactly how I wanted it (based on index), but great for looping through the data with the functions I wrote.不完全是我想要的(基于索引),但非常适合使用我编写的函数遍历数据。

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

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