Phani
Phani

Reputation: 41

Google Spreadsheet to CSV in Google Drive

While uploading CSV file to Google drive, it automatically converting to Google Sheets. How to save it as CSV file in drive? or can I read google sheet through pandas data frame ?

Develop environment: Google Colab

Code Snippet:

Input

data = pd.read_csv("ner_dataset.desktop (3dec943a)", 
encoding="latin1").fillna(method="ffill")

data.tail(10)

Output

    [Desktop Entry]
0   Type=Link
1   Name=ner_dataset
2   URL=https://docs.google.com/spreadsheets/d/1w0...

Upvotes: 2

Views: 2756

Answers (2)

Phani
Phani

Reputation: 41

WORKING CODE

from google.colab import auth
auth.authenticate_user()

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())

worksheet = gc.open('Your spreadsheet name').sheet1

# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)

# Convert to a DataFrame and render.
import pandas as pd

pd.DataFrame.from_records(rows)

Upvotes: 2

user6882757
user6882757

Reputation:

#Mount the Drive
from google.colab import drive
drive.mount('drive')

#Authenticate you need to do with your credentials, fill yourself
gauth = GoogleAuth()

#Create CSV and Copy
df.to_csv('data.csv')
!cp data.csv drive/'your drive'

Upvotes: 1

Related Questions