Method Chaining
There is also a useful concept called method chaining in Pandas.
In method chaining, we can use more than one method together by connecting them with a dot (.).
The basic structure looks like this:
method1().method2().method3()
Here, the output of the first method becomes the input for the next method.
For example:
marks = pd.Series([85, 65, 95, 72, 90])
marks.sort_values().head(3)
output:
1 65
3 72
0 85
dtype: int64
Here, sort_values() first sorts the marks in ascending order:
65, 72, 85, 90, 95
Then, head(3) returns the first 3 values:
65, 72, 85
This allows us to perform multiple operations in a single line.
Converting DataFrame Column into a Series
A DataFrame contains multiple columns, and each individual column of a DataFrame is a Series.
So, if we want to extract a particular column from a DataFrame as a Series, we can simply select that column.
For example:
series_marks = df["Marks"]
Here, the Marks column is extracted from the DataFrame and stored in series_marks as a Series.
For example:
series_marks = df["Marks"]
type(series_marks)
output:
pandas.core.series.Series
So, selecting a single DataFrame column using df["Marks"] returns a Series.
Important Notes
Note 1: count() vs size: count() does not count missing values.
For example:
marks = pd.Series([80, 90, None, 70])
marks.size
output:
4
But count() returns only the number of non-null values:
marks.count()
output:
3
So remember:
size → total number of items, including missing values
count() → number of non-null items
Note 2: isna() and isnull(): isna() and isnull() work in the same way.
Similarly:
notna() and notnull() work in the same way.Note 3: unique() vs nunique(): unique() returns the actual unique values, while nunique() returns the number of unique values.
For example:
marks = pd.Series([85, 72, 90, 65, 72])
marks.unique()
output:
[85 72 90 65]
So, unique() returns the unique values.
marks.nunique()
output:
4
So, nunique() returns the number of unique values.
Note 4: head() vs tail(): head() works from the beginning of the Series, while tail() works from the end of the Series.
So:
head() → first few values
tail() → last few valuesNote 5: sort_values() vs sort_index(): sort_values() sorts the Series according to its values, while sort_index() sorts it according to its index.
Pandas Series Methods — Short Hand
Inspecting: head(), tail(), info(), describe(), unique(), nunique(), value_counts(), memory_usage()
Mathematical & Statistical: sum(), mean(), median(), mode(), min(), max(), count(), std(), var(), quantile(), cumsum(), cumprod(), cummax(), cummin()
Finding & Checking: isin(), between(), duplicated(), isna(), isnull(), notna(), notnull()
Missing Data: dropna(), fillna(), interpolate()
Sorting: sort_values(), sort_index(), nlargest(), nsmallest()
Transformation: apply(), map(), replace(), astype(), round(), clip()
Index & Position: reset_index(), set_axis(), reindex(), take()
String Data: str.lower(), str.upper(), str.title(), str.strip(), str.replace(), str.contains(), str.startswith(), str.endswith(), str.len(), str.split()
Combining & Comparing: combine(), combine_first(), compare(), equals()
Conversion & Export: to_list(), to_numpy(), to_dict(), to_frame(), to_string(), to_csv(), to_json()