![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Return a string of the type inferred from the values
To return a string of the type inferred from the values, use the index.inferred_type property in Pandas.
At first, import the required libraries −
import pandas as pd import numpy as np
Creating the index. For NaN, we have used numpy library −
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])
Display the index −
print("Pandas Index...\n",index)
Return a string of the type inferred from the values −
print("\nThe inferred type...\n",index.inferred_type)
Example
Following is the code −
import pandas as pd import numpy as np # Creating the index # For NaN, we have used numpy library index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None]) # Display the index print("Pandas Index...\n",index) # Return an array representing the data in the Index print("\nArray...\n",index.values) # Check if the index is having NaNs print("\nIs the Pandas index having NaNs?\n",index.hasnans) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # Return a string of the type inferred from the values print("\nThe inferred type...\n",index.inferred_type)
Output
This will produce the following code −
Pandas Index... Index(['Car', 'Bike', nan, 'Car', nan, 'Ship', None, None], dtype='object') Array... ['Car' 'Bike' nan 'Car' nan 'Ship' None None] Is the Pandas index having NaNs? True The dtype object... object The inferred type... Mixed
Advertisements