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:
JavaScript
x
5
1
import pandas as pd
2
3
url = 'https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'
4
dfs = pd.read_html(url)
5
How can we read this file in pandas?
Related links:
Advertisement
Answer
Using pandas
JavaScript
1
8
1
import pandas as pd
2
3
url='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'
4
file_id=url.split('/')[-2]
5
dwn_url='https://drive.google.com/uc?id=' + file_id
6
df = pd.read_csv(dwn_url)
7
print(df.head())
8
Using pandas and requests
JavaScript
1
13
13
1
import pandas as pd
2
import requests
3
from io import StringIO
4
5
url='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'
6
7
file_id = url.split('/')[-2]
8
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
9
url2 = requests.get(dwn_url).text
10
csv_raw = StringIO(url2)
11
df = pd.read_csv(csv_raw)
12
print(df.head())
13
output
JavaScript
1
7
1
sex age state cheq_balance savings_balance credit_score special_offer
2
0 Female 10.0 FL 7342.26 5482.87 774 True
3
1 Female 14.0 CA 870.39 11823.74 770 True
4
2 Male 0.0 TX 3282.34 8564.79 605 True
5
3 Female 37.0 TX 4645.99 12826.76 608 True
6
4 Male NaN FL NaN 3493.08 551 False
7