Learn how to read a CSV file and filter rows based on specific column values using Python's pandas library.
import pandas as pd
def filter_csv_by_column_value(file_path, column_name, filter_value):
# Read the CSV file into a DataFrame
df = pd.read_csv(file_path)
# Filter the DataFrame based on the specified column value
filtered_df = df[df[column_name] == filter_value]
return filtered_df
# Example usage:
if __name__ == '__main__':
file_path = 'data.csv' # Path to your CSV file
column_name = 'status' # Column to filter by
filter_value = 'active' # Value to filter for
result = filter_csv_by_column_value(file_path, column_name, filter_value)
print(result)Views
Lines
Characters
Likes