pandas - how to organised dataframe based on date and assign new values to column

坚强是说给别人听的谎言 提交于 2019-12-13 13:40:36

问题


I have a dataframe of a month excluding Saturday and Sunday, which was logged every 1 minute.

                            v1         v2  
2017-04-03 09:15:00     35.7       35.4  
2017-04-03 09:16:00     28.7       28.5
      ...               ...        ...
2017-04-03 16:29:00     81.7       81.5
2017-04-03 16:30:00     82.7       82.6
      ...               ...        ...
2017-04-04 09:15:00     24.3       24.2  
2017-04-04 09:16:00     25.6       25.5
      ...               ...        ...
2017-04-04 16:29:00     67.0       67.2
2017-04-04 16:30:00     70.2       70.6
      ...               ...        ...
2017-04-28 09:15:00     31.7       31.4  
2017-04-28 09:16:00     31.5       31.0
      ...               ...        ...
2017-04-28 16:29:00     33.2       33.5
2017-04-28 16:30:00     33.0       30.7

I have resample dataframe to get 1st and last value from each day.

res = df.groupby(df.index.date).apply(lambda x: x.iloc[[0, -1]])
res.index = res.index.droplevel(0)
print(res)
                      v1    v2
2017-04-03 09:15:00  35.7  35.4
2017-04-03 16:30:00  82.7  82.6
2017-04-04 09:15:00  24.3  24.2
2017-04-04 16:30:00  70.2  70.6
   ...                ..    ..
2017-04-28 09:15:00  31.7  31.4
2017-04-28 16:30:00  33.0  30.7

Now i want to have the data-frame organised as date with v1 of minimum timestamp and v2 of max timestamp of specific date.

Desired output:

              v1    v2
2017-04-03  35.7  82.6
2017-04-04  24.3  70.6
   ...       ..    ..
2017-04-28  31.7  30.7

回答1:


You can groupby on index and use groupby.agg with a custom function.

df1 = res.groupby(res.index.date).agg({'v1': lambda x: x[min(x.index)], 'v2':lambda x: x[max(x.index)]})

print (df1)

             v1      v2
2017-04-03  35.7    82.6
2017-04-04  24.3    70.6
2017-04-28  31.7    33.7

An alternative to resample dataframe to get 1st and last value from each day.

res=df.reset_index().groupby(df.index.date).agg(['first','last']).stack().set_index('index')

Out[123]:

                      v1     v2
index       
2017-04-03 09:15:00  35.7   35.4
2017-04-03 16:30:00  82.7   82.6
2017-04-04 09:15:00  24.3   24.2
2017-04-04 16:30:00  70.2   70.6
2017-04-28 09:15:00  31.7   31.4
2017-04-28 16:30:00  33.0   33.7



回答2:


Try this:

df_result = pd.DataFrame()
df_result['v1'] = res.groupby(res.index)['v1'].min()
df_result['v2'] = res.groupby(res.index)['v2'].max()



回答3:


There is a very interesting fonction in pandas to work with the datetime index. It is the resampling fonction. In your Case try this :

def first_last(entry):
   return entry['v1'][0],entry['v2'][1]

yourdataframe.resample('D').apply(first_last)

the 'D' stands for Daily resampling.

result :

Dates                 
2017-04-03  35.7  82.6
2017-04-04  24.3  70.6



回答4:


You can reset_index and then GroupBy + apply with a custom function:

def first_second(x):
    return pd.Series({'v1': x['v1'].iat[0], 'v2': x['v2'].iat[-1]})

res2 = res.reset_index()
res2 = res2.groupby(res2['index'].dt.date).apply(first_second)

print(res2)

              v1    v2
index                 
2017-04-03  35.7  82.6
2017-04-04  24.3  70.6
2017-04-28  31.7  33.7


来源:https://stackoverflow.com/questions/52913816/pandas-how-to-organised-dataframe-based-on-date-and-assign-new-values-to-colum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!