I have problems with dropping values from dataframe with MultiIndex in Python, Pandas. I want to set condition to one of the indexes. However, in the multtindex, df.index.levels[0] prints shorter list of indexes than there really are (only unique values, not values for each row).
import pandas as pd
index = [int(a//3) for a in list(range(0, 6, 1))]
index2 = list(range(0, 6, 1))
val = list(range(100, 106, 1))
df = pd.DataFrame({'col1': index, 'col2': index2, 'col3': val})
df.set_index(['col1', 'col2'], inplace=True)
print(df)
print(df[df.index.levels[0] == 0])
I obtain error:
raise ValueError(
ValueError: Item wrong length 2 instead of 6.
I also thought about dropping this level from index by .reset_index(drop=False, inplace=True), selecting the correct values and going back with this col to index again - but it seems like a way around the problem...