Introduction to Pandas: Creating Series and DataFrames, CSV I/O, Filtering and Sorting
This tutorial introduces Pandas fundamentals, demonstrating how to create Series and DataFrames, read and write CSV files, perform basic filtering and sorting, and includes practical code examples and a hands‑on exercise to display the first five rows of a CSV dataset.
Goal : Familiarize with Pandas data structures and basic operations.
Learning Content : Creation of Series and DataFrames; reading and writing CSV files; basic data filtering and sorting.
Code Examples :
import pandas as pd1. Create Series
# Create Series from list
data_list = [1, 2, 3, 4, 5]
series = pd.Series(data_list)
print(f"Series created from list:\n{series}") # Create Series from dict
data_dict = {'a': 10, 'b': 20, 'c': 30}
series_dict = pd.Series(data_dict)
print(f"Series created from dict:\n{series_dict}")2. Create DataFrames
# Create DataFrame from dict
data_dict = {
'Name': ['Zhang San', 'Li Si', 'Wang Wu'],
'Age': [25, 30, 35],
'City': ['Beijing', 'Shanghai', 'Guangzhou']
}
df = pd.DataFrame(data_dict)
print(f"DataFrame created from dict:\n{df}") # Create DataFrame from list
data_list = [
['Zhang San', 25, 'Beijing'],
['Li Si', 30, 'Shanghai'],
['Wang Wu', 35, 'Guangzhou']
]
columns = ['Name', 'Age', 'City']
df_list = pd.DataFrame(data_list, columns=columns)
print(f"DataFrame created from list:\n{df_list}")3. Read and Write CSV Files
# Read CSV file
file_path = 'example.csv'
df = pd.read_csv(file_path)
print(f"Read CSV file:\n{df}") # Write CSV file
output_file_path = 'output.csv'
df.to_csv(output_file_path, index=False) # Do not save index
print(f"CSV file written to: {output_file_path}")4. Basic Data Filtering
# Create a DataFrame for filtering
data_dict = {
'Name': ['Zhang San', 'Li Si', 'Wang Wu', 'Zhao Liu'],
'Age': [25, 30, 35, 40],
'City': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen']
}
df = pd.DataFrame(data_dict)
filtered_df = df[df['Age'] > 30]
print(f"Filtered DataFrame:\n{filtered_df}")5. Use loc and iloc for Selection
# Select with loc
selected_data_loc = df.loc[df['City'] == 'Beijing', ['Name', 'Age']]
print(f"Data selected with loc:\n{selected_data_loc}") # Select with iloc
selected_data_iloc = df.iloc[1:3, 0:2]
print(f"Data selected with iloc:\n{selected_data_iloc}")6. Basic Data Sorting
# Sort by a single column
sorted_df = df.sort_values(by='Age')
print(f"DataFrame sorted by Age:\n{sorted_df}") # Sort by multiple columns
sorted_df_multi = df.sort_values(by=['Age', 'City'])
print(f"DataFrame sorted by Age and City:\n{sorted_df_multi}")Practice : Read a CSV file and display the first five rows.
# Import Pandas library
import pandas as pd
# Read CSV file
file_path = 'example.csv'
df = pd.read_csv(file_path)
# Show first 5 rows
print(f"First 5 rows:\n{df.head(5)}")Summary : Through today's practice you should have mastered how to use Pandas to create and manipulate Series and DataFrames, read and write CSV files, and perform basic data filtering and sorting. In the coming days we will continue to explore more advanced Python data‑processing techniques.
Test Development Learning Exchange
Test Development Learning Exchange
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.