Reputation: 1329
There seem to be plenty of other questions about indexing Pandas DataFrames, but I have not found a way to do the type of changes I want. If I have a DF which looks likes
Value
Index1 Index2
0 1 1.1
1 2 1.2
2 3 2.4
3 1 1.3
4 2 2.2
5 3 3.1
I don't need all of index1 to be unique. I would rather have something like
Value
Index1 Index2
0 1 1.1
0 2 1.2
0 3 2.4
1 1 1.3
1 2 2.2
1 3 3.1
Is there a way to do this? I think the easiest way is to apply a function of dividing by index1 values by 3, but not sure how you apply a function to an index. Perhaps though pandas has it's own methods for redefining index values to have groupings like this which are still unique when you consider both indexes?
Upvotes: 1
Views: 560
Reputation: 879321
import io
import pandas as pd
text = '''\
Index1 Index2 Value
0 1 1.1
1 2 1.2
2 3 2.4
3 1 1.3
4 2 2.2
5 3 3.1'''
df = pd.read_table(io.BytesIO(text), sep='\s+', index_col=[0, 1])
df.index = pd.MultiIndex.from_tuples(
[(item[0] // 3, item[1]) for item in df.index],
names=df.index.names)
print(df)
yields
Value
Index1 Index2
0 1 1.1
2 1.2
3 2.4
1 1 1.3
2 2.2
3 3.1
Upvotes: 5