【问题标题】:Can't pull dataframe head after sorting out data整理数据后无法拉数据帧头
【发布时间】:2020-08-09 06:44:40
【问题描述】:

我正在尝试为一个项目整理一些社区数据,但我不太明白我做错了什么。

import requests
import lxml.html as lh
import pandas as pd

data = pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')[0]

data.head() #Works

torsort = data[ (data['Borough'] == 'Not assigned') & (data['Neighborhood'] == 'NaN') ].index

data.drop(indexNames , inplace=True)

torsort.head() #Does not work

错误:AttributeError:“Int64Index”对象没有属性“head”

我不明白发生了什么以及为什么我看不到这个新数据帧的标题。

我能否对此进行解释,以及如何理解并继续验证我的数据过滤是否有效?

谢谢!

  • 我刚刚意识到一个错误,我在 data.drop 上输入了 indexNames 而不是 torsort,但我仍然没有提取数据。

【问题讨论】:

  • 最后不应该是data.head()吗?
  • 正如错误提示的那样,torsort 是一个“Int64Index”类型的对象,而不是一个数据框。

标签: python pandas dataframe web-scraping header


【解决方案1】:

你的意思可能是这样的:

torsort = data[ (data['Borough'] == 'Not assigned') & (data['Neighborhood'].isnull()) ].index

data.drop(data.index[torsort])

输出:

Postal code     Borough     Neighborhood
2   M3A     North York  Parkwoods
3   M4A     North York  Victoria Village

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2021-09-15
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    相关资源
    最近更新 更多