w3resource

Pandas Pivot Titanic: Extract the column labels, shape and data types of the dataset

Pandas: Pivot Titanic Exercise-2 with Solution

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                                       

Python Code Editor:

Pivot Titanic.csv:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to print a concise summary of the dataset (titanic.csv).
Next: Write a Pandas program to create a Pivot table with multiple indexes from the data set of titanic.csv.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.