简体   繁体   English

使用熊猫从URL读取导入的CSV文件时出错

[英]Error while reading imported csv file from url with pandas

I'm a beginner trying to advance on a project that I learned with a tutorial. 我是一个初学者,试图推进我通过教程学到的项目。 The project consists on importing a csv file from the United States Geological Survey and plotting its data on a map. 该项目包括从美国地质调查局导入一个csv文件并将其数据绘制在地图上。

I managed to make it while using a file that's located in my computer. 我设法使用计算机上的文件来制作它。 However, I cannot get around on getting the csv directly from the url, so that the data can update itself. 但是,我无法直接从url获取csv,因此数据可以自行更新。

Right now I'm using pandas and getting this error: 现在我正在使用熊猫并收到此错误:

File "C:/Users/Felipe/PycharmProjects/earthquake/earthquake.py", line 6, in with open(filename) as csvfile: TypeError: expected str, bytes or os.PathLike object, not DataFrame 文件“ C:/Users/Felipe/PycharmProjects/earthquake/earthquake.py”,第6行,以open(filename)作为csvfile:TypeError:预期的str,字节或os.PathLike对象,而不是DataFrame

import pandas as pd

filename = pd.read_csv('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.csv')
lats, lons = [], []

with open(filename) as f:
    reader = csv.reader(f)
    next(reader)
    for row in reader:
        lats.append(float(row[1]))
        lons.append(float(row[2]))

print('lats', lats[0:5])
print('lons', lons[0:5])

This part of the code is where I'm trying to retrieve de csv file. 这部分代码是我尝试检索de csv文件的地方。

I believe (again, I'm a beginner) that pandas actually gives me the data already "processed" (not as a csv anymore), and the tools I'm trying to use are usable on for csv files itself (this is me trying to understand the error, and I'm sure I'm probably wrong, but I couldn't think of anything else). 我相信(再次,我是一个初学者),pandas实际上给了我已经“处理过”的数据(不再是csv了),而我尝试使用的工具可用于csv文件本身(这就是我试图理解错误,我敢肯定我可能是错的,但是我什么也没想到。

I tried looking at pandas documentations but couldn't find much infomation about "DataFrame" error. 我尝试查看熊猫文档,但找不到有关“ DataFrame”错误的很多信息。 I also tried to use the ".to_csv" after importing the file, but it didn't work. 导入文件后,我也尝试使用“ .to_csv”,但是没有用。

So, tl;dr how can I get around this code and import a csv file and use its data? 因此,tl; dr如何解决该代码并导入csv文件并使用其数据?

I also have some extra questions that I couldn't find the answer to: on "with open(filename) as f:" what does the "f" do? 我还有一些其他问题,找不到答案:在“使用open(filename)as f:”上,“ f”有什么作用?

Thanks a lot! 非常感谢!

I believe the filename should just be the name. 我相信文件名应该只是名称。

filename = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.csv'

This should get it into a dataframe: 这应该将其放入数据框:

import io
import requests

content = requests.get(filename).content
df = pd.read_csv(io.StringIO(content.decode('utf-8')))

or simply: 或者简单地:

df = pd.read_csv(filename)

pd.read_csv() also accepts URLs and returns parsed Pandas DataFrame. pd.read_csv()也接受URL并返回已解析的Pandas DataFrame。

Demo: 演示:

In [55]: url = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.csv'

In [56]: df = pd.read_csv(url)

In [57]: df
Out[57]:
                       time   latitude  longitude   depth   mag magType  nst  gap    dmin   rms    ...                      updated  \
0  2017-09-12T20:38:20.330Z -15.079900 -174.19000  144.26  4.80      mb  NaN   71  2.6100  0.85    ...     2017-09-12T20:56:45.040Z
1  2017-09-12T20:22:02.350Z  39.878502 -121.27433    6.78  2.74      md  9.0  215  0.3287  0.14    ...     2017-09-12T20:36:02.917Z

                             place        type horizontalError depthError  magError  magNst     status  locationSource magSource
0       107km NNW of Hihifo, Tonga  earthquake            8.80       5.30     0.033     285   reviewed              us        us
1  27km ENE of Magalia, California  earthquake            1.82      10.49     0.240       8  automatic              nc        nc

[2 rows x 22 columns]

In [58]: df[['latitude','longitude']]
Out[58]:
    latitude  longitude
0 -15.079900 -174.19000
1  39.878502 -121.27433

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM