How To Import Data File In Python

One of the key tasks in data analysis and machine learning is to read data from different types of files such as CSV, Excel, JSON, etc. In this tutorial, we will learn how to import data from various file formats into Python using different libraries. Specifically, we will cover how to read data from CSV, Excel, and JSON files using the pandas library.

Step 1: Install Pandas library

Before we begin, we need to install the pandas library, if you haven’t done so already. You can install it using pip by running the following command:

Step 2: Import pandas library in Python

To use pandas in your Python script or Jupyter notebook, you need to import it first. You can do this by adding the following line of code at the beginning of your script or notebook:

Step 3: Read data from a CSV file

To read data from a CSV file, you can use the read_csv() function from the pandas library. For example, if you have a CSV file called sample.csv with the following content:

id,name,age
1,John,25
2,Jane,28
3,Mark,30

You can read the data from this file into a pandas DataFrame object using the following code:

   id  name  age
0   1  John   25
1   2  Jane   28
2   3  Mark   30

Step 4: Read data from an Excel file

To read data from an Excel file, you can use the read_excel() function from the pandas library. For example, if you have an Excel file called sample.xlsx with the same content as before:

   id  name  age
0   1  John   25
1   2  Jane   28
2   3  Mark   30

Step 5: Read data from a JSON file

To read data from a JSON file, you can use the read_json() function from the pandas library. For example, if you have a JSON file called sample.json with the following content:

[
  {"id": 1, "name": "John", "age": 25},
  {"id": 2, "name": "Jane", "age": 28},
  {"id": 3, "name": "Mark", "age": 30}
]

You can read the data from this file into a pandas DataFrame object using the following code:

   id  name  age
0   1  John   25
1   2  Jane   28
2   3  Mark   30

Full code:

Conclusion

In this tutorial, we have learned how to import data from CSV, Excel, and JSON files using the pandas library in Python. This allows you to easily work with different types of data files to perform data analysis and machine learning tasks. You can now use pandas to manipulate, clean, and analyze the imported data in your Python projects.