25 Essential pandas Tricks for Data Manipulation in Python
This article presents a comprehensive collection of 25 practical pandas techniques, covering version inspection, DataFrame creation, column renaming, prefix/suffix addition, row and column reversal, dtype selection, type conversion, memory optimization, and efficient construction of DataFrames from multiple CSV files.
This article shares 25 useful pandas tricks for Python data analysis, illustrating how to inspect the pandas version and its dependencies with pd.__version__ and pd.show_versions() .
It demonstrates creating example DataFrames from a dictionary and from numpy.random.rand() , and how to assign custom column names.
Column renaming is covered using df.rename() , direct assignment to df.columns , and df.columns.str.replace() to replace spaces with underscores.
Adding prefixes or suffixes to column names is shown with df.add_prefix() and df.add_suffix() .
Row order can be reversed with df.loc[::-1] and reset to a default integer index using df.loc[::-1].reset_index(drop=True) .
Column order reversal is achieved via df.loc[:, ::-1] , which swaps the column positions.
Selecting columns by data type is illustrated with df.select_dtypes(include='number') for numeric columns and similar calls for object types.
Converting object columns to numeric types uses df[col].astype() , pd.to_numeric() with errors='coerce' , and filling missing values with df.fillna(0) or applying the conversion across the DataFrame with df.apply(pd.to_numeric, errors='coerce') .
Memory usage can be reduced by reading only needed columns via the usecols parameter and converting suitable object columns to the category dtype, shrinking a DataFrame from 30.4 KB to 2.3 KB.
For datasets split across multiple CSV files, the article shows how to concatenate them row‑wise using glob.glob() , sorted() , and pd.concat(..., ignore_index=True) , as well as column‑wise concatenation with pd.concat(..., axis=1) .
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.