Home >Backend Development >Python Tutorial >How to read CSV files using the Pandas library
How to read CSV files with Pandas
Overview:
CSV (Comma-Separated Values) is a common spreadsheet file format that starts with commas or other specific characters as delimiters for field values. Pandas is a powerful data processing library that can easily read, process and analyze various data files, including CSV files. This article will introduce how to use the Pandas library to read CSV files and give specific code examples.
Steps:
Import the required libraries
import pandas as pd
First, we need to import the Pandas library.
Read CSV files using Pandas’ read_csv function
data = pd.read_csv('file_path.csv')
In this step, we use the read_csv function to read CSV files. You need to replace file_path.csv with the path and file name of your actual file. This function will load the file contents into a DataFrame object named data.
If the field separator in the CSV file is not a comma, but other characters, you can use the sep parameter to specify the separator. For example, if the delimiter is a semicolon, the code is as follows:
data = pd.read_csv('file_path.csv', sep=';')
View data
print(data.head())
By using the head function, we can print out the first few rows of the data set, to view the data content. The default parameter of the head function is 5, indicating to print out the first five lines of data.
View the dimensions of the data (number of rows and columns)
print(data.shape)
The shape attribute can return the dimension information of the DataFrame, for example (rows number, number of columns).
View column names
print(data.columns)
The columns property can return a list of column names of the DataFrame.
View the statistical summary of the data
print(data.describe())
The describe function can return the statistical summary information of the data, including mean, standard deviation, minimum value, maximum value, etc.
Filter data
For example, we can filter data to obtain a subset of data under specific conditions:
filtered_data = data[data['column_name'] > 10]
In the above example, we filtered out the columns Data named 'column_name' with a value greater than 10.
Sort data
sorted_data = data.sort_values(by='column_name', ascending=True)
Through the sort_values function, we can sort the data, sort according to the specified column name, and specify ascending or descending order.
Save data
data.to_csv('new_file_path.csv', index=False)
The to_csv function can save the DataFrame object as a new CSV file. You need to replace new_file_path.csv with the file name and path you actually want to save. The index=False parameter indicates that the index of the data is not saved.
Summary:
This article introduces the steps of how to use Pandas to read CSV files, and gives specific code examples. Pandas provides a wealth of functions and methods that can easily process and analyze data. By using these features, we can make better use of the data in CSV files.
The above is the detailed content of How to read CSV files using the Pandas library. For more information, please follow other related articles on the PHP Chinese website!