Q:

Flexible relative value calculation in python / ValueError: cannot reindex from a duplicate axis

I want to use the value of df.d to define the row from which I calculate the relative value by using the formula df.a/df.a[x] while x is defined as df.d. But somehow this doesn't work. My approach so far is this one:
import pandas as pd
import numpy as np
import datetime

randn = np.random.randn
rng = pd.date_range('1/1/2011', periods=10, freq='D')

df = pd.DataFrame({'a': [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0], 'b': [1.1, 1.7, 1.3, 1.6, 1.5, 1.1, 1.5, 1.7, 2.1, 1.9],'c':[None] * 10},index=rng)

df["d"]= [0,0,0,0,4,4,4,4,8,8]
df["c"] =df.a/df.a[df.d]

All I get is the error: ValueError: cannot reindex from a duplicate axis
 
To clarify this: I want to set df.a/df.a[0] for the first 4 rows, df.a/df.a[4] for the next 4 and df.a/df.a[8] for the last 2 rows according to df["d"]= [0,0,0,0,4,4,4,4,8,8]
So how can I refer to a value in the dataframe correcty, without getting this error?
The output I seek looks like this:
              a    b     c                       d
2011-01-01  1.1  1.1  1                          0   #  df.a/df.a[0] 
2011-01-02  1.2  1.7  1.090909090909091          0   #  df.a/df.a[0] 
2011-01-03  1.3  1.3  1.181818181818182          0   #  df.a/df.a[0] 
2011-01-04  1.4  1.6  1.272727272727273          0   #  df.a/df.a[0] 
2011-01-05  1.5  1.5  1                          4   #  df.a/df.a[4] 
2011-01-06  1.6  1.1  1.066666666666667          4   #  df.a/df.a[4] 
2011-01-07  1.7  1.5  1.133333333333333          4   #  df.a/df.a[4] 
2011-01-08  1.8  1.7  1.2                        4   #  df.a/df.a[4] 
2011-01-09  1.9  2.1  1                          8   #  df.a/df.a[8] 
2011-01-10  2.0  1.9  1.052631578947368          8   #  df.a/df.a[8] 

The pandas version used is 0.16.0 
Thanks a lot for your support!

A:

With regards to your original Error, I get a different error - 
Unsupported Iterator Index.  That's 'cos I am trying to get values from df.a at an index which is a series (df.d) and not an Index value. (I've pandas version 0.13.1), but to solve your actual problem - 
Here's how I could go about it. 
df['d'] = pd.Series([0,0,0,0,4,4,4,4,8,8], index=rng)
x = df.a.iloc[df.d]

note here - x you get has a different date index  so simply 
df['c'] = df.a/x # incorrect

won't work. we are only interested in values - so we take them out and assign (ignoring the index). 
df['c'] = df.a/x.values # We ignore the index of 'x' 

or as a short form
df['c'] = df.a/df.a.iloc[df.d].values 

What is not clear to me yet is - even though the index of df.d is correct why simple df.a.iloc won't work.
Hope that helps. 

