Pandas Pivot Titanic: Extract the column labels, shape and data types of the dataset
2. Extract Dataset Info (Columns, Shape, Data Types)
Write a Pandas program to extract the column labels, shape and data types of the dataset (titanic.csv). Go to Editor
Sample Solution:
Python Code :
import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
print("List of columns:")
print(df.columns)
print("\nShape of the Dataset:")
print(df.shape)
print("\nData types of the Dataset:")
print(df.dtypes)
Sample Output:
List of columns:
Index(['survived', 'pclass', 'sex', 'age', 'sibsp', 'parch', 'fare',
'embarked', 'class', 'who', 'adult_male', 'deck', 'embark_town',
'alive', 'alone', 'Unnamed: 15'],
dtype='object')
Shape of the Dataset:
(891, 16)
Data types of the Dataset:
survived int64
pclass int64
sex object
age float64
sibsp int64
parch int64
fare float64
embarked object
class object
who object
adult_male bool
deck object
embark_town object
alive object
alone bool
Unnamed: 15 float64
dtype: object
For more Practice: Solve these Related Problems:
- Write a Pandas program to import titanic.csv and extract its column labels, shape, and data types using the dtypes attribute.
- Write a Pandas program to load titanic.csv and print the list of column names along with the DataFrame's dimensions.
- Write a Pandas program to read titanic.csv and create a summary that includes column labels, the DataFrame’s shape, and each column’s data type.
- Write a Pandas program to import titanic.csv and verify the data types of all columns by iterating over the DataFrame’s columns.
Go to:
PREV : Concise Summary of Titanic Dataset.
NEXT : Pivot Table with Multiple Indexes from Titanic.
Python Code Editor:
Pivot Titanic.csv:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
