Hi I am unable to read CSV file from the URL by using
import pandas as pd
import numpy as np
data_url = 'https://data.baltimorecity.gov/Financial/Real-Property-Taxes/27w9-urtv.csv'
df = pd.read_csv(data_url)
df.head()
I got an error: "not acceptable"
I also tried different codes importing "requests" but none of them worked. How do I fix this?
Your URL wasnt correct. This should work:
import pandas as pd
data_url = 'https://data.baltimorecity.gov/resource/27w9-urtv.csv'
df = pd.read_csv(data_url)
df.head()
Related
I'm trying to read csv file in pandas from this url:
https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv
By doing this:
url = "https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv">
c = pd.read_csv(url)
Or by doing this:
import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
And i still get the same error message:
ParserError: Error tokenizing data. C error: Expected 1 fields in line 3, saw 2
Simply:
import pandas as pd
df = pd.read_csv("https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv?dl=1")
print(df)
You require a ?dl=1 at the end of your link.
Related
Add ?dl=1 to the end of the URL
import pandas as pd
url = "https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv?dl=1"
c = pd.read_csv(url)
print(c)
How to download dropbox csv file to pandas
I am trying to read a json, which I get from the python package 'yahoofinancials' (it pulls the data from Yahoo Finance):
import numpy as np
import pandas as pd
from yahoofinancials import YahooFinancials
yahoo_financials = YahooFinancials(ticker)
cash_statements = yahoo_financials.get_financial_stmts('annual', 'income')
cash_statements
pd.read_json(str(cash_statements).replace("'", '"'), orient='records')
However I get the error:
Unexpected character found when decoding 'NaN'
The problem is this command: str(cash_statements).replace("'", '"').
You tried to "convert" from a python dictionary to a json string, by replacing single with double quotes, which does not properly work.
Use the json.dump(cash_statements) function for converting your dictionary object into a json string.
Updated Code:
import numpy as np
import pandas as pd
from yahoofinancials import YahooFinancials
# ADJUSTMENT 1 - import json
import json
# just some sample data for testing
ticker = ['AAPL', 'MSFT', 'INTC']
yahoo_financials = YahooFinancials(ticker)
cash_statements = yahoo_financials.get_financial_stmts('annual', 'income')
# ADJUSTMENT 2 - dict to json
cash_statements_json = json.dumps(cash_statements)
pd.read_json(cash_statements_json, orient='records')
Check whether the file is available or the file name is correct because I got the same error while reading a .json file that was not in that folder and located somewhere else.
I'm new to python and I got this error I couldn't solve
import pandas as pd
import numpy as np
url = 'http://localhost:8888/edit/Downloads/untitled.cvs'
food2014_recalls = pd.read_csv(url)
This is my csv file:
animal,uniq_id,water_need
elephant,1001,500
elephant,1002,600
elephant,1003,550
I got this error:
import pandas as pd
import numpy as np
import io
import requests
url ='http://localhost:8888/edit/Downloads/untitled.cvs'
res =requests.get(url).content
food2014_recalls =pd.read_csv(io.StringIO(res.decode('utf-8')), error_bad_lines=False, comment='#', sep=',')
I'm still new to python. I'm trying to import an excel doc into python but I get the filenotfounderror
This is what I'm running:
import pandas as pd
practiceset = (r'C:\Users\michael\Desktop\Work\Transpo\'Transportation2016.xlsx')
df = pd.read_excel(practiceset)
print (df)
The python file is in the same folder as the doc, so I'm confused.
Try this:
import pandas as pd
practiceset = (r'C:\Users\michael\Desktop\Work\Transpo\Transportation2016.xlsx')
df = pd.read_excel(practiceset)
print (df)
I downloaded this dataset and stored it in a folder called AutomobileDataset.
I cross checked the working directory using:
import pandas as pd
import numpy as np
import os
os.chdir("/Users/madan/Desktop/ML/Datasets/AutomobileDataset")
os.getcwd()
Output:
'/Users/madan/Desktop/ML/Datasets/AutomobileDataset'
Then I tried reading the file using pandas:
import pandas as pd
import numpy as np
import os
os.chdir("/Users/madan/Desktop/ML/Datasets/AutomobileDataset")
os.getcwd()
automobile_data = pd.read_csv("AutomobileDataset.txt", sep = ',',
header = None, na_values = '?')
automobile_data.head()
Output:
---------------------------------------------------------------------------
ParserError: Error tokenizing data. C error: Expected 1 fields in line 2, saw 26
Someone please help me with this, I don't know where I am making a mistake.
Can try this!
import os
# Read in a plain text file
with open(os.path.join("c:user/xxx/xx", "xxx.txt"), "r") as f:
text = f.read()
print(text)