Pandas DataFrame - Get Axes Information
Axes of Pandas DataFrame
To get the axes information like index, name of the columns, and datatypes of these, you can use DataFrame.axes property.
In this tutorial, we shall learn how to get the DataFrame axes information.
Examples
1. Get axes of given DataFrame
In this example, we shall initialize a DataFrame with some rows. Then we shall call axes property on the DataFrame.
Python Program
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
axesInfo = df1.axes
print(axesInfo)
Output
D:\>python example.py
[Int64Index([0, 1, 2, 3], dtype='int64'), Index(['A', 'B', 'C', 'D'], dtype='object')]
2. Get axes of DataFrame created with no specific index
In this example, we have initialized a DataFrame without index. We shall observe what DataFrame.axes property returns.
Python Program
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
axesInfo = df1.axes
print(axesInfo)
Output
D:\>python example.py
[RangeIndex(start=0, stop=4, step=1), Index(['A', 'B', 'C', 'D'], dtype='object')]
A default index shall be created when you do not provide during DataFrame initialization. The same is reflected when we accessed DataFrame.axes property.
Summary
In this tutorial of Python Examples, we learned how to get information about the axes of a DataFrame using DataFrame.axes attribute of DataFrame class.