Skip to content Skip to sidebar Skip to footer

Pandas: How To Read Csv File From Google Drive Public?

I searched similar questions about reading csv from URL but I could not find a way to read csv file from google drive csv file. My attempt: import pandas as pd url = 'https://driv

Solution 1:

This worked for me

import pandas as pd
url='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'
url='https://drive.google.com/uc?id=' + url.split('/')[-2]
df = pd.read_csv(url)

Solution 2:

To read CSV file from google drive you can do that.

import pandas as pdurl='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'
path = 'https://drive.google.com/uc?export=download&id='+url.split('/')[-2]
df = pd.read_csv(path)

I think this is the easiest way to read CSV files from google drive. hope your "Anyone with the link" option enables in google drive.

Solution 3:

I would recommend you using the following code:

import pandas as pd
import requests
from io importStringIO

url = requests.get('https://doc-0g-78-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/5otus4mg51j69f99n47jgs0t374r46u3/1560607200000/09837260612050622056/*/0B6GhBwm5vaB2ekdlZW5WZnppb28?e=download')
csv_raw = StringIO(url.text)
dfs = pd.read_csv(csv_raw)

hope this helps

Solution 4:

Using pandas

import pandas as pd

url='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'
file_id=url.split('/')[-2]
dwn_url='https://drive.google.com/uc?id=' + file_id
df = pd.read_csv(dwn_url)
print(df.head())

Using pandas and requests

import pandas as pd
import requests
from io importStringIO

url='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'

file_id = url.split('/')[-2]
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
url2 = requests.get(dwn_url).text
csv_raw = StringIO(url2)
df = pd.read_csv(csv_raw)
print(df.head())

output

sexagestatecheq_balancesavings_balancecredit_scorespecial_offer0Female10.0FL7342.26          5482.87           774True1Female14.0CA870.3911823.74770True2Male0.0TX3282.34          8564.79           605True3Female37.0TX4645.99         12826.76608True4MaleNaNFLNaN3493.08           551False

Solution 5:

Simply change de URL from Google Drive using uc?id=, and then pass it to the read_csv function. In this example:

url = 'https://drive.google.com/uc?id=0B6GhBwm5vaB2ekdlZW5WZnppb28'dfs = pd.read_csv(url)

Post a Comment for "Pandas: How To Read Csv File From Google Drive Public?"