熊猫-从日期时间数据框中获取每天的第一个和最后一个值

时间:2018-10-20 19:59:08

标签: python pandas pandas-groupby

我有一个月(不包括星期六和星期日)的数据框,该数据框每1分钟记录一次。

def make_greeting(name, greeting = "Hello"):
    return (greeting + " " + name + "!")

def get_name():
    name_entry = input("enter a name: ")
    return name_entry

def get_greeting():
    greeting_entry = input("enter a greeting: ")
    return greeting_entry

# get name and greeting, send to make_greeting 
print(make_greeting(get_name(), get_greeting()))

如何重新采样数据框以获取每天的第一个和最后一个值。所需的数据帧:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal 
lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal 
hWnd1 As Long, ByVal hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As 
String) As Long
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal 
hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal 
lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) 
As Long
'Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd 
As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" 
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam 
As String) As Long


Const SW_NORMAL = 1
Const WM_SETTEXT = &HC

Sub Test()
Dim x, hwnd As Long, TLogin As Long, subWindow As Long

'hwnd = FindWindow(vbNullString, "Untitled - Notepad")
x = ShellExecute(hwnd, "open", "C:\Program Files\Skype\Phone\Skype.exe", 0, 
0, SW_NORMAL)
If x = 2 Or x = 3 Then Exit Sub

Do
    DoEvents
    hwnd = FindWindow("TLoginForm", vbNullString)
Loop Until hwnd > 0

Application.Wait Now + TimeValue("00:00:03")

'First Try
'    TLogin = FindWindowEx(hwnd, 0&, "TLoginAppControl", vbNullString)
'    subWindow = FindWindowEx(TLogin, 0&, "Shell Embedding", vbNullString)
'    subWindow = FindWindowEx(subWindow, 0&, "Shell DocObject View", vbNullString)
'    subWindow = FindWindowEx(subWindow, 0&, "Internet Explorer_Server", vbNullString)
'    Call SendMessageByString(subWindow, WM_SETTEXT, 0, "HelloWorld")


'Second Try
'    TLogin = FindWindowEx(hwnd, 0&, "TLoginControlBar", vbNullString)
'    Call SendMessageByString(TLogin, WM_SETTEXT, 0, "HelloWorld")

'Third Try
TLogin = FindWindowEx(hwnd, 0&, "TNativeLoginControl", vbNullString)
Call SendMessageByString(TLogin, WM_SETTEXT, 0, "HelloWorld")
End Sub

2 个答案:

答案 0 :(得分:4)

这是一种方法:

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  33.7

答案 1 :(得分:0)

1)使用df.groupby(column-name)按日期对数据帧进行分组 2)使用df.drop(..)删除每个组中除第一个和最后一个值以外的所有内容