在Pandas(Python)中使用GroupBy和DateTime

在Pandas(Python)中使用GroupBy和DateTime,python,pandas,datetime,Python,Pandas,Datetime,我从API(当然是JSON格式)中获得的数据如下所示: 其中第二列是以记号表示的日期/时间,第三列是某个值。我基本上有一天中每个小时的数据,几个月内每天的数据。现在,我想要实现的是,我想要得到每周第三列的最小值。我有下面的代码段,它正确地为我返回了最小值,但除了返回最小值之外,我还想返回特定的时间戳,作为该周发生的最低日期/时间。我如何修改下面的代码,以便还可以获得时间戳以及最小值 df = pandas.DataFrame(columns=['Timestamp', 'Value']) #

我从API(当然是JSON格式)中获得的数据如下所示:

其中第二列是以记号表示的日期/时间,第三列是某个值。我基本上有一天中每个小时的数据,几个月内每天的数据。现在,我想要实现的是,我想要得到每周第三列的最小值。我有下面的代码段,它正确地为我返回了最小值,但除了返回最小值之外,我还想返回特定的
时间戳
,作为该周发生的最低日期/时间。我如何修改下面的代码,以便还可以获得
时间戳
以及最小值

df = pandas.DataFrame(columns=['Timestamp', 'Value'])

# dic holds the data that I get from my API.
for i in range(len(dic)):
    df.loc[i] = [dic[i][1], dic[i][2]]

df['Timestamp'] = pandas.to_datetime(df['Timestamp'], unit='s')
df.sort_values(by=['Timestamp'])
df.set_index(df['Timestamp'], inplace=True)

df.groupby([pandas.Grouper(key='Timestamp', freq='W-MON')])['Value'].min()
我想您需要索引
min
时间戳的值
,然后选择by
loc

df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='s')

df = df.loc[df.groupby([pd.Grouper(key='Timestamp', freq='W-MON')])['Value'].idxmin()]
print (df)
            Timestamp  Value
2 2017-07-23 12:00:00   2902
详细信息

print (df.groupby([pd.Grouper(key='Timestamp', freq='W-MON')])['Value'].idxmin())
Timestamp
2017-07-24    2
Freq: W-MON, Name: Value, dtype: int64
print (df.groupby([pd.Grouper(key='Timestamp', freq='W-MON')])['Value'].idxmin())
Timestamp
2017-07-24    2
Freq: W-MON, Name: Value, dtype: int64