给出如下所示的DataFrame:
contractID
Date
1982-09-16 (SPZ1982, 1982-12-16 00:00:00)
1982-09-17 (SPZ1982, 1982-12-16 00:00:00)
1982-09-20 (SPZ1982, 1982-12-16 00:00:00)
...
2018-09-27 (SPZ2018, 2018-12-21 00:00:00)
2018-09-28 (SPZ2018, 2018-12-21 00:00:00)
2018-10-01 (SPZ2018, 2018-12-21 00:00:00)
我重复了contractID
,并且想对这些ID有效地执行操作,例如:
def query(df, tup, startDate, endDate):
ID = tup[0]
ExpirationDate = tup[1]
panel = df.loc[ID].loc[ExpirationDate].loc[startDate:endDate]
return panel
df = pd.DataFrame()
print('acquiring daily data...')
for tup in contractUse['contractID'].unique():
panel = query(rawData, tup, startDate, endDate)
if df.empty:
df = panel
else:
df = df.append(panel, verify_integrity=False)
return df
contractUse
是上面介绍的DataFrame。我只想遍历唯一值。当我遍历唯一值时,我需要获取该唯一值何时开始的索引值,以及该唯一值何时停止的索引值。然后,我将startDate
和endDate
的唯一值提供给我的query
函数。在熊猫中有快速的方法吗?
答案 0 :(得分:3)
您需要要做两件事:
foreach(var vName in listFromClass)
var imAlist = Select Name From TableName Where vName.filename Not Name
根据列将数据框分成多个块。因此,首先拆分您的.storycontent span{
visibility:hidden;
}
系列:
GroupBy
然后按新创建的contractID
系列分组:
df[['key', 'contract_date']] = pd.DataFrame(df.pop('contractID').values.tolist())
提取一个组的“开始和结束”索引现在就像提取一个组并查看其索引一样简单。例如:
key