CBSE Class 12 Informatics Practices Question 16 of 21

Python Pandas — II — Question 4

Back to all questions
4
Question

Question 4

Take a DataFrame of your choice. Write a program to calculate count of values only in a selective column.

Solution
import pandas as pd
data = {
    'Name': ['Deepika', 'Simran', 'Shivang', 'Anurag', 'Ankith'],
    'Age': [25, 30, None, 40, 45],
    'City': ['Goa', 'Kochi', 'Pondicherry', 'Rohtak', 'Kasol']
}

df = pd.DataFrame(data)
selected_column = 'Age'
count_values = df[selected_column].count()
print("Count of values in", selected_column, ":" , count_values)
Output
Count of values in Age : 4
Answer

import
pandas
as
pd
data
=
{
'Name'
: [
'Deepika'
,
'Simran'
,
'Shivang'
,
'Anurag'
,
'Ankith'
],
'Age'
: [
25
,
30
,
None
,
40
,
45
],
'City'
: [
'Goa'
,
'Kochi'
,
'Pondicherry'
,
'Rohtak'
,
'Kasol'
]
}
df
=
pd
.
DataFrame
(
data
)
selected_column
=
'Age'
count_values
=
df
[
selected_column
].
count
()
print
(
"Count of values in"
,
selected_column
,
":"
,
count_values
)
Output
Count of values in Age : 4