Python 数据帧-排序日期时出现问题
我已经阅读了很多关于这个主题的问题,但我似乎找不到我的代码不起作用的原因 在从一个网站上进行了一些抓取之后,我基本上是在尝试按列(“日期”)对熊猫数据帧进行排序。我曾尝试将日期转换为datetime对象,但也没有成功,如果你们中的任何人能告诉我我可能在这里做错了什么,我将不胜感激Python 数据帧-排序日期时出现问题,python,pandas,csv,sorting,beautifulsoup,Python,Pandas,Csv,Sorting,Beautifulsoup,我已经阅读了很多关于这个主题的问题,但我似乎找不到我的代码不起作用的原因 在从一个网站上进行了一些抓取之后,我基本上是在尝试按列(“日期”)对熊猫数据帧进行排序。我曾尝试将日期转换为datetime对象,但也没有成功,如果你们中的任何人能告诉我我可能在这里做错了什么,我将不胜感激 def find_data(soup): l = [] for b in soup.find_all('div', class_= 'jobInfo'): d = {}
def find_data(soup):
l = []
for b in soup.find_all('div', class_= 'jobInfo'):
d = {}
company = b.find('h2').find('a')
d["Role"] = company['title'].split(':')[0]
d["URL"] = 'https://www.computerjobs.ie' + company['href']
company_name = b.find('ul', class_= 'jobDetails').find('li', class_= 'jobCompanyName').get_text()
d["Company"] = company_name.split(':')[1].strip()
date = b.find('ul', class_= 'jobDetails').find('li', class_= 'jobLiveDate').get_text()
d["Date"] = date.split(':')[1].strip()
l.append(d)
df = pd.DataFrame(l)
#Rearranging the order of the columns
df = df[['Date', 'Company', 'Role', 'URL']]
#Dropping null rows
df=df.dropna()
#df['Date'] = pd.to_datetime(df.Date)
#df.sort_values('by=["'Date"], ascending = True)
df.sort_values(by = ['Date'])
df.to_csv("csv_files/pandas_data.csv")
输出:
,Date,Employer,Title,URL
0,11/04/2018,nineDots - Technology Recruitment,Senior Python Developer,https://www.computerjobs.ie/jobs/7175653/senior-python-developer.asp
1,10/04/2018,Allen Recruitment,Lead Python Developer,https://www.computerjobs.ie/jobs/7158984/lead-python-developer.asp
2,10/04/2018,Allen Recruitment,Python Developer,https://www.computerjobs.ie/jobs/7158996/python-developer.asp
3,11/04/2018,Solas Consulting Group,Python Developer,https://www.computerjobs.ie/jobs/7231476/python-developer.asp
4,11/04/2018,nineDots - Technology Recruitment,Senior Python Developer,https://www.computerjobs.ie/jobs/7181828/senior-python-developer.asp
5,09/04/2018,realTime Recruitment Ltd.,Senior DevOps Engineer,https://www.computerjobs.ie/jobs/7240215/senior-devops-engineer.asp
6,11/04/2018,FRS Recruitment,Software Engineer/Cloud Engineer,https://www.computerjobs.ie/jobs/7140213/software-engineer-cloud-engineer.asp
7,11/04/2018,Solas Consulting Group,Junior .NET Developer,https://www.computerjobs.ie/jobs/7232494/junior-net-developer.asp
8,11/04/2018,Evolve Adviser Ltd,Data Architect,https://www.computerjobs.ie/jobs/7247685/data-architect.asp
9,11/04/2018,nineDots - Technology Recruitment,Senior DevOps Engineer,https://www.computerjobs.ie/jobs/7191814/senior-devops-engineer.asp
重新分配排序语句:
df = df.sort_values(by = ['Date'])
默认情况下,df.sort_值不在原位,您必须重新分配回df以保留排序,或者在
sort_值
方法中使用inplace=True
作为参数。如果显示代码的输出,可能会有所帮助。这是不合适的。您需要执行df=df.sort_值(按=['Date'])
或df.sort_值(按=['Date'],inplace=True)
添加inplace=True谢谢大家的评论。这正是问题所在,我忘记将其重新分配给df以保留排序。我很欣赏添加'inplace=True'的附加选项。@pyrish另外,请注意,您不应该同时执行这两项操作。如果使用inplace=True,则函数的返回值将为null/NoneType。要么重新赋值给变量,要么使用inplace=True。没错,我只使用了inplace=True,它解决了日期排序问题。再次感谢@Scott Boston确保我做对了。