AskHandle Blog
Beginner's Guide to Using the Pandas Python Library
- Table Reading
- Generative AI
- AI

Beginner's Guide to Using the Pandas Python Library
Pandas is a Python library designed for data manipulation and analysis. It provides powerful data structures such as DataFrames and Series that make data cleaning, analysis, and visualization easier.
Installing Pandas
Ensure Python is installed on your system, then install Pandas using pip:
1pip install pandasStarting with Pandas
Import Pandas in your Python script or Jupyter notebook:
1import pandas as pdBasic Commands in Pandas
-
Creating a DataFrame: Create a DataFrame from a Python dictionary:
python1data = {'Name': ['John', 'Anna', 'Peter'], 'Age': [28, 34, 29]} 2df = pd.DataFrame(data) 3print(df) -
Reading a CSV File: Read data from a CSV file into a DataFrame:
python1df = pd.read_csv('path/to/your/file.csv') -
Inspecting Data: Get an overview of your DataFrame:
python1df.head() # First 5 rows 2df.tail() # Last 5 rows 3df.describe() # Statistical summary -
Selecting Data: Select columns or rows:
python1df['Name'] # 'Name' column 2df.iloc[0] # First row -
Filtering Data: Filter data based on conditions:
python1df_filtered = df[df['Age'] > 30] # Rows where age is over 30 -
Exporting Data to CSV: Save your processed data back to a CSV file:
python1df_filtered.to_csv('path/to/your/output.csv', index=False)This saves your filtered DataFrame (
df_filtered) as a new CSV file. Theindex=Falseparameter prevents Pandas from writing row indices into the CSV file.
A Full Example of Using Pandas
This Python script demonstrates filtering people above the age of 30 from a CSV file and exporting the results to a new CSV file. The filtered data is saved in a file named filtered_data.csv.
| Name | Age |
|---|---|
| Anna | 34 |
| Lisa | 42 |
| Tom | 31 |
1import pandas as pd
2
3# Reading data from the 'filtered_data.csv' file
4df = pd.read_csv('/path/to/filtered_data.csv')
5
6# Filtering for people with age above 30
7df_filtered = df[df['Age'] > 30]
8
9# Exporting the filtered data to a new CSV file
10output_file_path = '/mnt/data/filtered_data.csv'
11df_filtered.to_csv(output_file_path, index=False)
12
13output_file_pathUseful Resources
Pandas is a powerful and user-friendly tool for data analysis in Python. It streamlines various data-related tasks, making data manipulation efficient and straightforward.
(Edited on September 4, 2024)