Dataframe.loc returns dictionary or a Dataframe [Solved] (Cannot handle a non-unique multi-index!)
I've figured out that .loc returns a Dataframe or an Dictionary-like object depending on if there are duplicated indexes. This condition is not explained in the pandas documentation or I've not find it.
If the index are actually unique try using something along this code:
df.reset_index().drop_duplicates(subset=["index1"]).set_index(["index1"])
or just df.drop_duplicates(subset=["index1"]) after reading the csv but before setting the index
I had two dataframes that are being read from two almost identical .csv using pd.read_csv().
When I use .loc[index1] on one of them it returns a Dictionary such as:
col1        val1
col2        val2
col3        val3
Name: (index1), dtype: object
But with the other I've realized it actually returns a Dataframe. Some operations such as df1[col1] = df2[col2] + constant will through errors.
To make it even harder I'm actually using MultiIndex. I'm getting this error:
Cannot handle a non-unique multi-index!