【问题标题】:Column Does not Show Up in Pandas?专栏不显示在 Pandas 中?
【发布时间】:2022-11-19 00:37:52
【问题描述】:

这是我们正在使用的代码;基本上只是从多个废弃的数据集中获取数据,然后将它们连接起来。

import pandas as pd
import numpy as np # for numeric python functions
from pylab import * # for easy matplotlib plotting
from bs4 import BeautifulSoup
import requests
url1='http://openinsider.com/screener?s=&o=&pl=&ph=&ll=&lh=&fd=30&fdr=&td=0&tdr=&fdlyl=&fdlyh=&daysago=&xp=1&vl=&vh=&ocl=&och=&sic1=-1&sicl=100&sich=9999&grp=0&nfl=&nfh=&nil=&nih=&nol=&noh=&v2l=&v2h=&oc2l=&oc2h=&sortcol=0&cnt=100&page=1'
df1 = pd.read_html(url1)
table=df1[11]
#the table works - now lets make it look at change owned to find the largest value
#sorting
n = np.quantile(table['Qty'], [0.50])
print("99th percentile: ",n)
q=table.sort_values('Qty', ascending = False)
page = requests.get(url1)
name=q['Ticker'].str.replace('\d+', '')
name1 = (table['Ticker'])
n = name1.count()
#Buyers for the company
All = []
url = 'http://openinsider.com/'
for entry in name1:
  table2 = pd.read_html(url+entry)
  dfn=table2[11]
  All.append(dfn)
All = pd.concat(All)
print(All.columns)#<- my sanity check
print(All['Insider Name'])#<- where the problem lies

现在,如果您查看串联的数据集,您将看到“Insider Name”列。我想隔离此列,但是当我这样做时,python 说:


KeyError: 'Insider Name'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'Insider Name'

所以该列存在,但它也不存在?任何提示将非常感谢! 提前致谢!

【问题讨论】:

  • print(All.columns) 显示什么?

标签: python pandas web-scraping data-science


【解决方案1】:

尝试:

All.columns = All.columns.str.strip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多