Pandas Basics
Before pandas, handling real-world, often messy data in Python was cumbersome. But pandas simplifies it all by providing high-performance, easy-to-use data structures. At its heart are two main objects: the DataFrame and the Series.
What are DataFrames and Series?
Think of a DataFrame as a spreadsheet or a SQL table, a two-dimensional structure with rows and columns, where each column can hold a different type of data (e.g., numbers, text, or dates). A Series, on the other hand, is a single column of a DataFrame—a one-dimensional labeled array that can hold any data type.
Getting Started with Pandas
To begin, you'll need to install pandas and import it into your Python environment. The standard practice is to use the alias
pd for convenience.Your First DataFrame
Creating a DataFrame from scratch is simple. You can use a Python dictionary where keys become column headers and values are the column data.
Output:
Reading Data from a File
Pandas really shines when you need to load data from external files. It can read various formats like CSV, Excel, and SQL databases with a single line of code.
Basic Data Exploration
Once your data is in a DataFrame, you can start exploring it. Here are some essential commands:
df.head(): Displays the first 5 rows of your DataFrame.df.tail(): Shows you the last 5 rows.df.info(): Provides a concise summary of the DataFrame, including the data types of each column and the number of non-null values.df.describe(): Generates descriptive statistics for numerical columns, giving you a quick overview of your data's distribution.df.shape: Returns a tuple representing the dimensions (rows, columns) of the DataFrame.
Column Selection and Manipulation
You can easily select, rename, or add columns to your DataFrame.
Select a single column (returns a Series):
Select multiple columns (returns a DataFrame):
Add a new column:
Filtering and Sorting
Filtering data based on conditions and sorting by values are core features of pandas.
Filter rows where 'Age' is greater than 30:
Sort by 'Salary' in descending order:
Comments
Post a Comment