Pandas read_csv() Function
Pandas read_csv() method reads the specified CSV
file into DataFrame
.
pandas.read_csv()
grammar
pandas.read_csv(filepath_or_buffer: Union[str, pathlib.Path, IO[~ AnyStr]],
sep=',',
delimiter=None,
header='infer',
names=None,
index_col=None,
usecols=None,
squeeze=False,
prefix=None,
mangle_dupe_cols=True,
dtype=None,
engine=None,
converters=None,
true_values=None,
false_values=None,
skipinitialspace=False,
skiprows=None,
skipfooter=0,
nrows=None,
na_values=None,
keep_default_na=True,
na_filter=True,
verbose=False,
skip_blank_lines=True,
parse_dates=False,
infer_datetime_format=False,
keep_date_col=False,
date_parser=None,
dayfirst=False,
cache_dates=True,
iterator=False,
chunksize=None,
compression='infer',
thousands=None,
decimal: str='.',
lineterminator=None,
quotechar='"',
quoting=0,
doublequote=True,
escapechar=None,
comment=None,
encoding=None,
dialect=None,
error_bad_lines=True,
warn_bad_lines=True,
delim_whitespace=False,
low_memory=True,
memory_map=False,
float_precision=None)
parameter
filepath_or_buffer |
Location of the CSV file to import |
delimiter |
Delimiter used to parse CSV file content |
usecols |
When forming from a CSV file DataFrame , only the column names are included. |
header |
Which row/rows are used as column names for the headers? |
squeeze |
If the parsed data contains only one column, a Pandas is returned Series . |
skiprows |
Which line(s) to skip |
Return Value
Dataframe formed from a CSV file with labeled axes.
Example code: Pandas uses pandas.read_csv()
the function to read CSV
files
import pandas as pd
df = pd.read_csv("dataset.csv")
print(df)
Output:
Country Item Type Sales Channel Order Priority
0 Tuvalu Baby Food Offline H
1 East Timor Meat Online L
2 Norway Baby Food Online L
3 Portugal Baby Food Online H
4 Honduras Snacks Online L
5 New Zealand Fruits Online H
6 Moldova Personal Care Online L
This method CSV
loads the file into DataFrame
. Here, we can use both absolute and relative paths to provide a file path as pandas.read_csv()
a parameter of the function.
In this case, dataset.csv
it is in the same directory as the program file, which means that you can use CSV
the file name as the file path.
Example code: pandas.read_csv()
Setting usecols
parameters in a function
import pandas as pd
df = pd.read_csv("dataset.csv",usecols=["Country","Sales Channel","Order Priority"])
print(df)
Output:
Country Sales Channel Order Priority
0 Tuvalu Offline H
1 East Timor Online L
2 Norway Online L
3 Portugal Online H
4 Honduras Online L
5 New Zealand Online H
6 Moldova Online L
This example loads a file into by including only usecols
the specified columns in the parameter .CSV
DataFrame
Country
, Sales Channel
and Order Priority
these columns are only passed as parameters, so they are only included in DataFrame
.
pandas.read_csv()
Example code: Function with header
import pandas as pd
df = pd.read_csv("dataset.csv",header=1)
print(df)
Output:
Tuvalu Baby Food Offline H
0 East Timor Meat Online L
1 Norway Baby Food Online L
2 Portugal Baby Food Online H
3 Honduras Snacks Online L
4 New Zealand Fruits Online H
5 Moldova Personal Care Online L
header
The procedure loads CSV
the file into by setting line 1 to DataFrame
.
Here, the first row elements are used as DataFrame
the column names of the entire .
pandas.read_csv()
Example code: Function with line skipping
import pandas as pd
df = pd.read_csv("dataset.csv",skiprows=3)
print(df)
Output:
Norway Baby Food Online L
0 Portugal Baby Food Online H
1 Honduras Snacks Online L
2 New Zealand Fruits Online H
3 Moldova Personal Care Online L
This process loads the CSV file into a DataFrame by skipping the first 3 rows.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:123 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50
Pandas Convert String to Number
Publish Date:2025/04/12 Views:147 Category:Python
-
This tutorial explains how to pandas.to_numeric() convert string values of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :
How to Change the Data Type of a Column in Pandas
Publish Date:2025/04/12 Views:139 Category:Python
-
We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method
Get the first row of Dataframe Pandas
Publish Date:2025/04/12 Views:78 Category:Python
-
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get