Importing comma-separated values (CSV) files into a Pandas DataFrame is a common task in data analysis. To achieve this, Pandas provides a convenient function: pandas.read_csv.
How to Use pandas.read_csv
To read a CSV file into a DataFrame, simply call the read_csv function and provide the path to the file as an argument. Here's a simple example:
import pandas as pd df = pd.read_csv("data.csv")
Once the DataFrame is created, you can access its contents using the usual methods:
print(df)
This will output:
Date price factor_1 factor_2 0 2012-06-11 1600.20 1.255 1.548 1 2012-06-12 1610.02 1.258 1.554 2 2012-06-13 1618.07 1.249 1.552 3 2012-06-14 1624.40 1.253 1.556 4 2012-06-15 1626.15 1.258 1.552 5 2012-06-16 1626.15 1.263 1.558 6 2012-06-17 1626.15 1.264 1.572
Additional Options
pandas.read_csv also supports various optional arguments to customize the loading process. For example, you can specify the separator used in the CSV file using the sep argument:
df = pd.read_csv("data.csv", sep="|")
Additionally, you can skip specific rows in the CSV file using the skiprows argument:
df = pd.read_csv("data.csv", skiprows=1)
The above is the detailed content of How Can I Import CSV Files into Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!