How to Import a CSV File Into Python Using Pandas?

The “csv” file, also referred to as “comma separated value”, used commas to separate the text. The “csv” file allows us to store tabular data such as spreadsheets as simple plain text. Python provides a pandas package that is utilized for the analysis and manipulation of data. For efficient and easy data analysis, the “pd.read_csv()” function is used to import the csv data into Python. 

The following methods are used to import the “CSV” file into Python. 

How to Import a CSV File into Python Using pd.read_csv() Function?

To import a CSV file into Python utilizing the pandas, you can use the “pd.read_csv()” function. The following examples demonstrate how to import CSV files in Python:

Our csv file named “ILF.csv” contains the following data:

Example 1: Import CSV File Content into Python

In the below example code, the “pandas” module function “pd.read_csv()” is used to read the specific file from the Python working directory.

Code:

import pandas
output = pandas.read_csv(r'ILF.csv')
print(output)

In the above code:

  • The “pandas.read_csv()” function accepts the “csv” file name “ILF.csv” and displays the content of the csv file as a Pandas DataFrame in the output window.

Output:

The above output shows the data from the selected “csv” file.

Example 2: Import Specific Data From CSV File into Python

The “pd.read_csv()” function is used along with the “pd.DataFrame()” to import the specific data from the selected csv file in Python.

Code:

import pandas
data_frame = pandas.read_csv(r'ILF.csv')
output = pandas.DataFrame(data_frame, columns=['Name', 'Height'])
print(output)

In the above code:

  • The “pd.read_csv()” function is used to read the “csv” file named “ILF.csv”.
  • The “pd.DataFrame()” function accepts the return value of the “pd.read_csv() “ function as a first argument and a specific column name as a second argument.
  • The columns mentioned in the “pd.DataFrame()” function will be displayed on the output.

Output:

The above output shows the “csv” file data having column names “Name” and “Height”.

Example 3: Import CSV File Data into Python With New Column Names

In the below code, the “csv” file data is imported into Python with a new column name using the “columns”  parameter of the “pd.read_csv()” function.

Code:

import pandas
output = pandas.read_csv(r'ILF.csv', names=['students','new_age','new_height'])
print(output)

In the above code:

  • The “pd.read_csv()” function accepts the “ILF.csv” file and new columns name as an argument.
  • The csv file is imported into Python with new column names at the header of DataFrame.

Output:

The above output shows the “ILF.csv” file data with the new column names.

Conclusion

To import csv files into python using pandas, the “pd.read_csv()” function is used. We can also import particular data from a csv file into Python. The “pd.read_csv()” function can import data with new column names using the columns parameter. This Python tutorial detailed a guide on importing a csv file into Python.