How to get the names (titles or labels) of a pandas data frame in python ?

Daidalos October 21, 2019


Examples of how to get the names (titles or labels) of a pandas data frame in python

Get the row names of a pandas data frame

Let's consider a data frame called df. to get the row names a solution is to do:

>>> df.index

Get the row names of a pandas data frame (Exemple 1)

Let's create a simple data frame:

>>> import pandas as pd
>>> import numpy as np
>>> data = np.arange(1,13)
>>> data = data.reshape(3,4)
>>> data
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> columns = ['Home','Car','Sport','Food']
>>> index = ['Alice','Bob','Emma']
>>> df = pd.DataFrame(data=data,index=index,columns=columns)
>>> df
       Home  Car  Sport  Food
Alice     1    2      3     4
Bob       5    6      7     8
Emma      9   10     11    12

To get the names of the data frame rows:

>>> df.index
Index(['Alice', 'Bob', 'Emma'], dtype='object')

Get the row names of a pandas data frame (Exemple 2)

Another example using the csv file train.csv (that can be downloaded on kaggle):

>>> import pandas as pd
>>> df = pd.read_csv('train.csv')
>>> df.index
RangeIndex(start=0, stop=1460, step=1)

Select rows of a pandas data frame

Example 1:

>>> import pandas as pd
>>> import numpy as np
>>> data = np.arange(1,13)
>>> data = data.reshape(3,4)
>>> columns = ['Home','Car','Sport','Food']
>>> index = ['Alice','Bob','Emma']
>>> df = pd.DataFrame(data=data,index=index,columns=columns)
>>> df
       Home  Car  Sport  Food
Alice     1    2      3     4
Bob       5    6      7     8
Emma      9   10     11    12
>>> df.loc['Bob',:]
Home     5
Car      6
Sport    7
Food     8
Name: Bob, dtype: int64
>>> df.loc['Bob',['Car','Food']]
Car     6
Food    8
Name: Bob, dtype: int64

Example 2:

>>> import pandas as pd
>>> df = pd.read_csv('train.csv')
>>> df.loc[0,:]
Id                     1
MSSubClass            60
MSZoning              RL
LotFrontage           65
LotArea             8450
Street              Pave
Alley                NaN
LotShape             Reg
LandContour          Lvl
Utilities         AllPub
LotConfig         Inside
LandSlope            Gtl
Neighborhood     CollgCr
Condition1          Norm
Condition2          Norm
BldgType            1Fam
HouseStyle        2Story
OverallQual            7
OverallCond            5
YearBuilt           2003
YearRemodAdd        2003
RoofStyle          Gable
RoofMatl         CompShg
Exterior1st      VinylSd
Exterior2nd      VinylSd
MasVnrType       BrkFace
MasVnrArea           196
ExterQual             Gd
ExterCond             TA
Foundation         PConc
                  ...   
BedroomAbvGr           3
KitchenAbvGr           1
KitchenQual           Gd
TotRmsAbvGrd           8
Functional           Typ
Fireplaces             0
FireplaceQu          NaN
GarageType        Attchd
GarageYrBlt         2003
GarageFinish         RFn
GarageCars             2
GarageArea           548
GarageQual            TA
GarageCond            TA
PavedDrive             Y
WoodDeckSF             0
OpenPorchSF           61
EnclosedPorch          0
3SsnPorch              0
ScreenPorch            0
PoolArea               0
PoolQC               NaN
Fence                NaN
MiscFeature          NaN
MiscVal                0
MoSold                 2
YrSold              2008
SaleType              WD
SaleCondition     Normal
SalePrice         208500
Name: 0, dtype: object

References

Links Site
Index pandas doc
Select Rows & Columns by Name or Index in DataFrame using loc & iloc Python Pandas thispointer.com
Different ways to create Pandas Dataframe geeksforgeeks
pandas.DataFrame pandas.pydata.org
read_csv pandas.pydata.org

Licence


Activity


Google Ads