Wednesday, 14 September 2022

Dataframe columns having individual indices

I have noticed an interesting behaviour which I haven't seen in the documentation:

Each column inside a dataframe can have its individual index!

df = pd.DataFrame(np.arange(12).reshape(4, 3, order='F'),
                  columns=list('abc'))

df
   a  b   c
0  0  4   8
1  1  5   9
2  2  6  10
3  3  7  11

Assign index to column b:

df['b'].index = [-1, 2, 4, 5]

Different indices for different columns, but they all share the same dataframe index:

df['a']
0    0
1    1
2    2
3    3
Name: a, dtype: int64

df['b']
-1    4
 2    5
 4    6
 5    7
Name: b, dtype: int64


df.loc[:2, ['b']]
   b
0  4
1  5
2  6

df.loc[:2, 'b']
-1    4
 2    5
Name: b, dtype: int64

Is this described somewhere in the documentation?

Why can this be done in the first place? And can this be useful for something?



from Dataframe columns having individual indices

No comments:

Post a Comment