Python 为什么我的pandas数据框只包含第一列和最后一列?
我最近刚刚学习了web开发的基础知识。但是,当有4列时,我的pandas dataframe仅显示第一列和最后一列。节目如下:Python 为什么我的pandas数据框只包含第一列和最后一列?,python,html,pandas,dataframe,python-requests,Python,Html,Pandas,Dataframe,Python Requests,我最近刚刚学习了web开发的基础知识。但是,当有4列时,我的pandas dataframe仅显示第一列和最后一列。节目如下: import requests import pandas as pd r= requests.get('https://www.nytimes.com/interactive/2017/06/23/opinion/trumps-lies.html') from bs4 import BeautifulSoup soup=BeautifulSoup(r.text
import requests
import pandas as pd
r= requests.get('https://www.nytimes.com/interactive/2017/06/23/opinion/trumps-lies.html')
from bs4 import BeautifulSoup
soup=BeautifulSoup(r.text,'html.parser')
results=soup.find_all('span',attrs={'class':'short-desc'})
print(len(results))
print(results[0:3])
first_result=results[0]
print(first_result.find('strong'),'\n')
print(first_result.find('strong').text,'\n')
print(first_result.find('strong').text+', 2017','\n\n')
print('\t\tExtracting The Lie\n')
print(first_result.contents[1][0:-1],'\n\n')
print('\t\tExtracting The Explanation\n')
print(first_result.contents[2],'\n')
print(first_result.find('a'),'\n')
print(first_result.find('a').text[1:-1],'\n\n')
print('\t\tExtracting The URL\n')
print(first_result.find('a')['href'],'\n')
print('\t\tBuilding a Dataset\n')
records=[]
for result in results:
date=result.find('strong').text[0:-1]+', 2017'
lie=result.contents[1][1:-2]
explanation=result.find('a').text[1:-1]
url=result.find('a')['href']
records.append((date,lie,explanation,url))
print(len(records))
print(records[0:3],'\n\n')
df=pd.DataFrame(records,columns=['date','lie','explanation','url'])
print(df.head())
除了熊猫,一切都正常运转。前五列显示如下:
date ... url
0 Jan. 21, 2017 ... https://www.buzzfeed.com/andrewkaczynski/in-20...
1 Jan. 21, 2017 ... http://nation.time.com/2013/11/06/10-things-yo...
2 Jan. 23, 2017 ... https://www.nytimes.com/2017/01/23/us/politics...
3 Jan. 25, 2017 ... https://www.nytimes.com/2017/01/21/us/politics...
4 Jan. 25, 2017 ... https://www.nytimes.com/2017/01/24/us/politics...
我使用的是pycharm,熊猫的版本是1.0.4。为什么用“…”来代替文本?为了更好地显示它,
..
只是一个缩写。实际值没有省略号
要验证是否可以使用df.iloc[0]
打印第一行,您的数据仍然存在。
这只是为了适应长柱子
看这里
打印(df.head())
日期谎言解释url
2017年1月21日我不是伊拉克的粉丝。我不想进去。。。在他反对入侵之前,他是支持入侵的。https://www.buzzfeed.com/andrewkaczynski/in-20...
2017年1月21日《时代》杂志的一名记者-我一直。。。特朗普上了11次封面,尼克松上了。。。http://nation.time.com/2013/11/06/10-things-yo...
2017年1月23日,300万到500万张非法选票。。。没有非法投票的证据。https://www.nytimes.com/2017/01/23/us/politics...
2017年1月25日3日现在,观众人数是有史以来最多的。但是。。。官方航拍照片显示奥巴马2009年就职典礼。。。https://www.nytimes.com/2017/01/21/us/politics...
2017年1月25日,请看皮尤报告(其中显示了vot…该报告从未提及选民欺诈)。https://www.nytimes.com/2017/01/24/us/politics...
>>>df.iloc[0,1]
“我不喜欢伊拉克。我不想去伊拉克。”