首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >KeyError: 0在将bs4 xml转换为熊猫df时

KeyError: 0在将bs4 xml转换为熊猫df时
EN

Stack Overflow用户
提问于 2020-03-26 14:44:40
回答 1查看 35关注 0票数 0

我正在尝试使用bs4将xml导入到熊猫。

bs4导入是可行的,但是让熊猫识别xml是有问题的。

代码语言:javascript
运行
复制
import requests
import bs4
import pandas as pd

url = 'https://www.federalreserve.gov/data.xml'
geturl = requests.get(url).text
data = bs4.BeautifulSoup(geturl, 'lxml')
df = pd.DataFrame(data)
print(df.head())

我期望df显示前5行数据,但是我得到了以下错误:

KeyError: 0

为什么熊猫要生产这个KeyError: 0?

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-26 15:13:06

xml文件中有五种不同的图表。你想要哪一个?这是一个使用第一个图表的例子:

代码语言:javascript
运行
复制
import requests
from bs4 import BeautifulSoup
import pandas as pd

# xml url
xml = 'https://www.federalreserve.gov/data.xml'
# GET request and create soup
r = requests.get(xml)
soup = BeautifulSoup(r.text, 'xml')

# list comprehension to create a list of all the charts in the xml file
charts = [chart for chart in soup.findAll('chart')]

# list comprehension to get the observation index and value of the first chart (i.e, charts[0])
data = [[ob['index'], ob['value']] for ob in charts[0].findAll('observation')]

# create DataFrame
df = pd.DataFrame(data, columns=['Date', 'Value'])
df.head()

        Date      Value
0   1-Aug-07  870261.00
1   8-Aug-07  865453.00
2  15-Aug-07  864931.00
3  22-Aug-07  862775.00
4  29-Aug-07  872873.00

更新

您可以遍历所有图表,并将其附加到一个数据集中。然后,您将按图表的标题调用每个DataFrame:

代码语言:javascript
运行
复制
import requests
from bs4 import BeautifulSoup
import pandas as pd

# xml url
xml = 'https://www.federalreserve.gov/data.xml'
# GET request and create soup
r = requests.get(xml)
soup = BeautifulSoup(r.text, 'xml')

# list comprehension to create a list of all the charts in the xml file
charts = [chart for chart in soup.findAll('chart')]

# empty dict
df_list = {}

for chart in charts:
    # list comprehension to get the observation index and value
    data = [[ob['index'], ob['value']] for ob in chart.findAll('observation')]
    # create DataFrame
    df = pd.DataFrame(data, columns=['Date', 'Value'])
    # create key from the the chart title and append df
    df_list[chart['title']] = []
    df_list[chart['title']].append(df)

# calling the second chart
df_list['Selected Assets of the Federal Reserve'][0].head()


        Date      Value
0   1-Aug-07  870261.00
1   8-Aug-07  865453.00
2  15-Aug-07  864931.00
3  22-Aug-07  862775.00
4  29-Aug-07  872873.00
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60869844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档