Informatics Practices · Ch 3 — Data Handling using Pandas – II
Calculating Number of Values
Calculating Number of Values
DataFrame.count() is the method used to find out how many non-null values exist in a DataFrame. It does not count missing (NaN) entries — only actual data. By default, when you call df.count(), it returns the count of non-null values for each column separately.
For example, if you have a DataFrame with 12 rows and columns like Name, UT, Maths, Science, S.St, Hindi, and Eng, calling df.count() will show you that each column has 12 values (assuming no data is missing). The output looks like this.
This tells you the total number of entries in each column.
Counting values row-wise
To count the number of values in each row instead of each column, you pass the argument axis=1. This is useful when you want to know how many subjects or fields a particular student has filled in.
The syntax is:
df.count(axis=1)
For a DataFrame with 12 rows and 7 columns (all filled), the output will be a Series where every row shows 7 — because each row has 7 non-null values:
0 7
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7
11 7
dtype: int64
Key point to remember
df.count()→ counts column-wise (axis=0 is the default).df.count(axis=1)→ counts row-wise.
Think and Reflect Activity …
| Column | Count |
|---|---|
| Name | 12 |
| UT | 12 |
| Maths | 12 |
| Science | 12 |
| S.St | 12 |
| Hindi | 12 |
Write a statement to count the number of values in a row. This contrasts count() with the default axis=0 (per column) against axis=1 (per row), confirming every student answered all …