Closed
Description
Depending on how the Dataframe is selected, Pandas does a transpose of what name
and index
refer to.
Namely given df.index
, it would naturally follow that selecting the index of the first element, df.iloc[0].index
would return df.index[0]
, but this isn't the case. Instead it returns the columns.
print(df.index)
# RangeIndex(start=0, stop=3, step=1)
print(df.iloc[0].index)
# Index(['a', 'b', 'c'], dtype='object')
See a more general example below:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(9).reshape(3,3), columns=['a', 'b', 'c'])
print(df.iloc[:, 0].index)
# output: RangeIndex(start=0, stop=3, step=1)
print(df.iloc[0, :].index)
# output: Index(['a', 'b', 'c'], dtype='object')
print(df.iloc[:, 0].name)
# output: a
print(df.iloc[0, :].name)
# output: 0
I think one should expect that index
should always refer to one thing and name
another regardless of how selection is made. The following may be more inline with expectation:
print(df.iloc[:, 0].index)
# output: RangeIndex(start=0, stop=3, step=1)
print(df.iloc[0, :].index)
# output: RangeIndex(start=0, stop=1, step=1)
print(df.iloc[:, 0].name)
# output: Index(['a'], dtype='object')
print(df.iloc[0, :].name)
# output: Index(['a', 'b', 'c'], dtype='object')
Metadata
Metadata
Assignees
Labels
No labels