Pandas Data Manipulation: Sorting, Selecting, Grouping, and Reshaping
This article provides a visual guide to using Pandas for data manipulation, covering sorting with sort_values, column selection, grouping with groupby and various aggregations, filtering rows and columns, dropping, joining, merging, pivot tables, melting, stacking, unstacking, and index operations, illustrated with code snippets and screenshots.
Pandas is a common tool for data mining; mastering its functions is essential. This guide demonstrates various Pandas operations through visual examples.
sort_values
<code>(dogs[dogs['size'] == 'medium']
.sort_values('type')
.groupby('type').median())
</code>Execution steps:
Filter rows where the size column equals "medium".
Convert the type column as needed.
Group by type and compute the median.
selecting a column
<code>dogs['longevity']
</code>groupby + mean
<code>dogs.groupby('size').mean()
</code>Execution steps:
Group data by the size column.
Perform aggregation (mean) within each group.
grouping multiple columns
<code>dogs.groupby(['type', 'size'])
</code>groupby + multi aggregation
<code>(dogs
.sort_values('size')
.groupby('size')['height']
.agg(['sum', 'mean', 'std']))
</code>Execution steps:
Sort data by the size column.
Group by size .
Calculate sum, mean, and standard deviation of height within each group.
filtering for columns
<code>df.loc[:, df.loc['two'] <= 20]
</code>filtering for rows
<code>dogs.loc[(dogs['size'] == 'medium') & (dogs['longevity'] > 12), 'breed']
</code>dropping columns
<code>dogs.drop(columns=['type'])
</code>joining
<code>ppl.join(dogs)
</code>merging
<code>ppl.merge(dogs, left_on='likes', right_on='breed', how='left')
</code>pivot table
<code>dogs.pivot_table(index='size', columns='kids', values='price')
</code>melting
<code>dogs.melt()
</code>pivoting
<code>dogs.pivot(index='size', columns='kids')
</code>stacking column index
<code>dogs.stack()
</code>unstacking row index
<code>dogs.unstack()
</code>resetting index
<code>dogs.reset_index()
</code>setting index
<code>dogs.set_index('breed')
</code>The article concludes with a brief disclaimer and a call‑to‑action to read the original source.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.