Pandas 3: File read and write operations

Pandas 3: File read and write operations, here we learn the third part of pandas’ tutorial.

we will discuss on pandas-read-and-write-operations in python. We need to know how to read files into pandas and convert it to pandas dataFrame to do data processing, also we need to know how to export or write the processed data to a file. files required for this exercise is uploaded to Google drive, we will see how we can access this data.

Connecting Google Drive to Google Colab is a convenient way to access data and files stored in your Google Drive directly within a Google Colab notebook. This is especially useful for working with datasets, files, or scripts that you want to use in your Colab environment. Here’s a step-by-step guide to help you connect Google Drive to Google Colab:

  1. Open Google Colab: Go to Google Colab and create a new notebook or open an existing one.
  2. Mount Google Drive: To access files from Google Drive in Colab, you need to mount your Google Drive to the Colab environment using the following code snippet. Run this code in a Colab cell:
from google.colab import drive
drive.mount('/content/drive')

When you run this code, a link will appear. Click on the link, allow access to your Google Drive, and you’ll receive an authorization code. Paste the code into the input field in the output cell. once mounted you will see as shown with green tick mark.

  1. Access Files: Once your Google Drive is successfully mounted, you can access your Google Drive files under the /content/drive/My Drive/ directory. For example, if you have a file named data.csv in the root of your Google Drive, you can access it in Colab using the path /content/drive/My Drive/data.csv.
data_path = '/content/drive/My Drive/data.csv'
  1. Work with Files: You can read, write, and manipulate files in Colab just like you would in a regular Python environment. For instance, you can use libraries like pandas to read CSV files, matplotlib to visualize data, etc.

import pandas as pd
data = pd.read_csv('/content/drive/My Drive/data.csv')
# Now you can work with the 'data' DataFrame

Saving Files: If you want to save files generated in Colab back to your Google Drive, you can do so using the same path structure.

Remember that the mounted Google Drive is specific to the current Colab session. If you close the notebook or your session times out, you will need to mount Google Drive again the next time you work in Colab. By following these steps, you’ll be able to seamlessly access and work with your Google Drive files within Google Colab for executing Python code and data analysis.

new_data = pd.DataFrame(...) # Your new data
new_data.to_csv('/content/drive/My Drive/new_data.csv', index=False)

we will see the python code to execute with colab python:

exercise Jupyter files can be downloaded from the path shown:

Examples

After going through these codes, go through the next lesson: object-oriented-programming

Was this article helpful?
YesNo

2 thoughts on “Pandas 3: File read and write operations”

Leave a Comment