Master Pandas Basics: From DataFrames to Quick Data Insights
This tutorial introduces Pandas fundamentals, covering installation, DataFrame creation, reading and storing CSV/Excel files, quick data inspection, column manipulation, handling different data types, and basic time series operations, providing a concise roadmap for beginners to start data analysis with Python.
01 Important Preface
This is the first article of the Python data analysis practical series, focusing on a simple encounter with Pandas. If you are already proficient with Pandas, you can skim or skip this content.
Many beginners quickly learn Python syntax but then dive straight into the classic "Python for Data Analysis" book, finishing it without truly understanding how to apply the knowledge. The main obstacles are insufficient understanding and lack of practice, often leading to a "three examples, one confusion" trap.
To avoid this, beginners should not overwhelm themselves with multiple methods for a single problem before mastering any.
02 Pandas Introduction
Pandas is a professional data analysis tool built on NumPy, offering flexible and efficient handling of various datasets. It provides two primary data structures: DataFrame (similar to an Excel sheet) and Series (a column in that sheet).
Before processing data, it is crucial to plan the analysis, clarify its purpose, and outline the workflow.
03 Create, Read, and Store
1. Create
First, import the library: import pandas as pd Construct a DataFrame using a dictionary of lists:
If no index is specified, Pandas generates a default integer index starting from 0.
2. Read
Read CSV files (use engine='python' to avoid encoding issues):
Read Excel files similarly:
Both functions support additional parameters such as header, sep, and names.
3. Store
Saving data is straightforward:
04 Quick Data Overview
1. View head and tail
Use df.head() to see the first few rows and df.tail() for the last rows. Both accept an integer argument to specify the number of rows.
2. Inspect data types df.info() displays column data types, non‑null counts, and memory usage.
3. Summary statistics df.describe() provides key statistics for numeric columns.
05 Basic Column Operations
Think of column operations as the four SQL actions: add, delete, select, update .
1. Add
Assign a new column:
df['new_column'] = values2. Delete
Use df.drop('column_name', axis=1, inplace=True) to remove a column.
3. Select
Single column: df['column']. Multiple columns:
df[['col1', 'col2']]4. Update
Modify a column directly:
df['existing_column'] = new_values06 Common Data Types and Operations
String
String operations are similar to native Python strings but require the .str accessor.
Example: removing a leading dash from a column of strings.
Numeric
Perform arithmetic directly on columns, e.g., adding a constant or computing a new metric:
df['sales'] = df['visitors'] * df['conversion_rate'] * df['avg_price']Be careful with mixed types; convert percentage strings to floats before calculations.
Datetime
Convert string dates to datetime objects with pd.to_datetime() and perform date arithmetic.
Recap
Understand what Pandas is.
Learn how to create, read, and store data.
Quickly inspect data with head, tail, info, and describe.
Perform basic add, delete, select, and update operations on columns.
Familiarize with common data types: strings, numerics, and datetimes.
Reply "初识pandas" to obtain the full case dataset.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
