Python Pandas – Get the datatype and DataFrame columns information


To get the datatype and DataFrame columns information, use the info() method. Import the required library with an alias −

import pandas as pd;

Create a DataFrame with 3 columns −

dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80]
   }
)

Get the datatype and other info about the DataFrame −

dataFrame.info()

Example

Following is the code −

import pandas as pd;

# create a DataFrame
dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80]
   }
)

print"DataFrame ...\n",dataFrame

# get unique values from a column
print"\nUnique values from a column ...\n",dataFrame['Car'].unique()
print"\nCount unique values from a column ...\n",dataFrame['Car'].nunique()

# get datatype info
print"\n Get the datatype info ...\n",dataFrame.info()

Output

This will produce the following output −

DataFrame ...
       Car        Place   Units
0      BMW       Delhi     100
1     Audi   Bangalore     150
2      BMW   Hyderabad      50
3    Lexus  Chandigarh     110
4    Tesla        Pune      90
5    Lexus      Mumbai     120
6  Mustang      Jaipur      80

Unique values from a column ...
['BMW' 'Audi' 'Lexus' 'Tesla' 'Mustang']

Count unique values from a column ...
5

Get the datatype info ...

RangeIndex: 7 entries, 0 to 6
Data   columns   (total 3 columns):
Car    7 non-null   object
Place  7 non-null   object
Units  7 non-null   int64
dtypes: int64(1), object(2)
memory usage: 240.0+ bytes
None

Updated on: 16-Sep-2021

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements