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://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing' dfs = pd.read_html(url)
How can we read this file in pandas?
Related links:
Advertisement
Answer
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 import StringIO 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
sex age state cheq_balance savings_balance credit_score special_offer 0 Female 10.0 FL 7342.26 5482.87 774 True 1 Female 14.0 CA 870.39 11823.74 770 True 2 Male 0.0 TX 3282.34 8564.79 605 True 3 Female 37.0 TX 4645.99 12826.76 608 True 4 Male NaN FL NaN 3493.08 551 False