1

Apologies for the fairly basic question.

Basically I have a large dataframe where I'm pulling out the top dates for the sum of certain values. Looks like this:

hv_toploss = hv.groupby(['END_VALID_DT']).sum()
hv_toploss=hv_toploss.sort_values('TOTALPL',ascending=False).iloc[:10]
hv_toploss['END_VALID_DT'] = pd.to_datetime(hv_toploss['END_VALID_DT'])

Now, END_VALID_DT becomes the index of hv_toploss, and I get a KeyError when running line 3. If I try to reindex, I get a multi-index error, and since these are the values I need, I can't just drop the index.

I will be calling these values in a line like:

PnlByDay = PnlByDay.loc[hv_toploss['END_VALID_DT']]

Any help here would be great. I'm still a novice using Python.

2 Answers2

0

Ok I got around this by just copying the index values into a new column and using that.

hv_toploss = hv.groupby(['END_VALID_DT']).sum()
hv_toploss['Scenario_Dates'] = hv_toploss.index
hv_toploss=hv_toploss.sort_values('TOTALPL',ascending=False).iloc[:10]

However any input on how to do this properly please advise.

0

You can use the index directly instead of creating another column containing the index.

the_dates = hv_toploss.sort_values('TOTALPL',ascending=False).iloc[:10].index
PnlByDay.loc[PnlByDay.index.isin(the_dates)]

I don't know the structure of PnlByDay, so you may have to modify that part.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223