Series Inspecting Methods
When we get a Series, the first thing we normally want to know is what data it contains. Pandas provides several methods that help us quickly inspect a Series.
head(): The head() method is used to see the first few values of the Series. By default, it returns the first 5 values.
For example:
marks = pd.Series([85, 72, 90, 65, 72, 88, 95])
marks.head()
output:
0 85
1 72
2 90
3 65
4 72
dtype: int64
If we want to see the first 3 values, we can pass 3 as an argument.
marks.head(3)
output:
0 85
1 72
2 90
dtype: int64
So, head() is mainly useful when we have a large Series and don't want to display the complete data.
tail(): The tail() method is similar to head(), but instead of returning the first few values, it returns the last few values. By default, it returns the last 5 values.
For example:
marks.tail()
If we want to see the last 3 values:
marks.tail(3)
output:
4 72
5 88
6 95
dtype: int64
So, we can remember it simply as:
head() → beginning of the Series
tail() → end of the Series
info(): The info() method gives us basic information about the Series.
For example:
It provides information such as the number of entries, index information, data type, and memory usage.
This is useful when we want to quickly understand the structure of a Series.
describe(): The describe() method gives us a statistical summary of the Series.
For example:
marks.describe()
For a numerical Series, it can provide information such as:
count
mean
standard deviation
minimum value
25th percentile
50th percentile
75th percentile
maximum value
So instead of manually calculating these values, describe() gives us a quick statistical summary.unique(): The unique() method returns all the unique values present in the Series.
For example, if the Series contains:
marks = pd.Series([85, 72, 90, 65, 72])
marks.unique()
output:
[85 72 90 65]
The duplicate 72 is returned only once.
nunique(): The nunique() method returns the number of unique values present in the Series.
For example, if the Series contains:
marks = pd.Series([85, 72, 90, 65, 72])
marks.nunique()
output:
4
Here, there are 4 unique values: 85, 72, 90, and 65.
So:
unique() → returns the actual unique values
nunique() → returns the number of unique values
value_counts(): The value_counts() method tells us how many times each unique value appears in the Series.
For example, if the Series contains:
marks = pd.Series([85, 72, 90, 65, 72])
marks.value_counts()
output:
72 2
85 1
90 1
65 1
dtype: int64
Here, 72 appears 2 times, while the other values appear once.
This method is especially useful when we want to understand the frequency of values in our data.
memory_usage(): The memory_usage() method tells us how much memory the Series is using.
For example:
marks.memory_usage()
output:
196
The exact value may vary depending on the Series and its data type.
This method is mainly useful when working with large datasets and we want to understand or optimize memory usage.