Skip to content
Pandas Data Manipulation: Sorting, Selecting, Grouping, and Reshaping Techniques
Fundamentals 4 min read

Pandas Data Manipulation: Sorting, Selecting, Grouping, and Reshaping Techniques

This article provides a comprehensive, visual guide to essential Pandas operations—including sorting, column selection, grouping, multi‑aggregation, filtering, dropping, joining, merging, pivot tables, melting, stacking, unstacking, and index manipulation—illustrated with clear code examples for data analysis in Python.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle

Pandas is a widely used tool for data mining, and mastering its functions is essential; this article visually demonstrates a variety of Pandas operations.

sort_values : Filter rows where the size column equals "medium", sort by type, then group by type and compute the median.

(dogs[dogs['size'] == 'medium'] .sort_values('type') .groupby('type').median())

Selecting a column : Access a single column directly, e.g., dogs['longevity'].

groupby + mean : Group data by size and calculate the mean of each group. dogs.groupby('size').mean() Grouping multiple columns : Perform a groupby on both type and size. dogs.groupby(['type', 'size']) groupby + multi aggregation : Sort by size, group by size, then aggregate the height column with sum, mean, and standard deviation.

(dogs.sort_values('size') .groupby('size')['height'].agg(['sum', 'mean', 'std']))

Filtering for columns : Use df.loc with a condition to select specific columns, e.g., df.loc[:, df.loc['two'] <= 20].

Filtering for rows : Combine multiple conditions to filter rows and select a column, e.g.,

dogs.loc[(dogs['size'] == 'medium') & (dogs['longevity'] > 12), 'breed']

.

Dropping columns : Remove unwanted columns with dogs.drop(columns=['type']).

Joining : Join two DataFrames using ppl.join(dogs).

Merging : Merge DataFrames on specific keys with a left join.

ppl.merge(dogs, left_on='likes', right_on='breed', how='left')

Pivot table : Create a pivot table to summarize data.

dogs.pivot_table(index='size', columns='kids', values='price')

Melting : Transform wide format to long format using dogs.melt().

Pivoting : Pivot the DataFrame with specified index and columns. dogs.pivot(index='size', columns='kids') Stacking column index : Stack the column index into a Series with dogs.stack().

Unstacking row index : Unstack the row index back to a DataFrame using dogs.unstack().

Resetting index : Reset the index to default integer indexing via dogs.reset_index().

Setting index : Set a specific column as the index, e.g., dogs.set_index('breed').

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondata analysissortingData ManipulationgroupingReshaping
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.