close

How to read csv to dataframe in Google Colab

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to read csv to dataframe in Google Colab in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to read csv to dataframe in Google Colab?

  1. How to read csv to dataframe in Google Colab?

    Pandas read_csv should do the trick. You'll want to wrap your uploaded bytes in an io.StringIO since read_csv expects a file-like object.

  2. read csv to dataframe in Google Colab

    Pandas read_csv should do the trick. You'll want to wrap your uploaded bytes in an io.StringIO since read_csv expects a file-like object.

Method 1

Pandas read_csv should do the trick. You’ll want to wrap your uploaded bytes in an io.StringIO since read_csv expects a file-like object.

The key snippet is:

import pandas as pd
import io

df = pd.read_csv(io.StringIO(uploaded['train.csv'].decode('utf-8')))
df

Method 2

Colab google: uploading csv from your PC I had the same problem with an excel file (*.xlsx), I solved the problem as the following and I think you could do the same with csv files: – If you have a file in your PC drive called (file.xlsx) then: 1- Upload it from your hard drive by using this simple code:

from google.colab import files
uploaded = files.upload()

Press on (Choose Files) and upload it to your google drive.

2- Then:

import io
data = io.BytesIO(uploaded['file.XLSX'])    

3- Finally, read your file:

import pandas as pd   
f = pd.read_excel(data , sheet_name = '1min', header = 0, skiprows = 2)
#df.sheet_names
df.head()

4- Please, change parameters values to read your own file. I think this could be generalized to read other types of files!
Enjoy it!

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read