w3resource

Pandas DataFrame: Display a summary of the basic information about a specified DataFrame and its data

Pandas: DataFrame Exercise-3 with Solution

Write a Pandas program to display a summary of the basic information about a specified DataFrame and its data.

Sample DataFrame:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Sample Solution :-

Python Code :

import pandas as pd
import numpy as np

exam_data  = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

df = pd.DataFrame(exam_data , index=labels)
print("Summary of the basic information about this DataFrame and its data:")
print(df.info())

Sample Output:

Summary of the basic information about this DataFrame and its data:
<class 'pandas.core.frame.DataFrame'>
Index: 10 entries, a to j
Data columns (total 4 columns):
attempts    10 non-null int64
name        10 non-null object
qualify     10 non-null object
score       8 non-null float64
dtypes: float64(1), int64(1), object(2)
memory usage: 400.0+ bytes
None                              

Explanation:

The above code creates a Pandas DataFrame object 'df' containing information about an exam, such as the name of the student, their score, the number of attempts, and whether they qualify.

The DataFrame is created using a Python dictionary 'exam_data' that contains lists of information about the students.

The 'labels' list is used to set the index of the DataFrame.

The DataFrame has four columns: 'name', 'score', 'attempts', and 'qualify'.

  • The 'name' column contains the names of the students.
  • The 'score' column contains the scores they received.
  • The 'attempts' column contains the number of attempts taken to pass the exam.
  • The 'qualify' column contains whether the student has qualified for the exam or not.

Finally the DataFrame is then printed using the print() function.

Python-Pandas Code Editor:

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

Previous: Write a Pandas program to create and display a DataFrame from a specified dictionary data which has the index labels.
Next: Write a Pandas program to get the first 3 rows of a given DataFrame.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/pandas/python-pandas-data-frame-exercise-3.php