python - Python DataFrame Filtering and Sorting at the Same Time
问题描述
Hi I have a data frame with column as following: 'founded' and 'company name'
What I'm trying to do is filtering the year founded > 0 and then sorting by company name, ascending.
I'm looking for a code similar to this
df_job_da_details_filter_sort = df_job_da_details[df_job_da_details['Founded'] > 0].sort_values(['Company Name'], ascending=True)
df_job_da_details_filter_sort.head()
But I got this error
IndentationError: unexpected indent
and at the moment I have this code:
df_job_da_details_year_cleaned = df_job_da_details[df_job_da_details['Founded'] > 0] #founded more than year 0
df_job_da_details_sort = df_job_da_details_year_cleaned.sort_values(['Company Name'], ascending=True) #sort by company name ascending
df_job_da_details_sort.head()
Are there any way that I can do the code like I intended to?
解决方案
The error in the code is syntactical rather than a logical one.
The way you are currently doing is correct and will produce the intended result
Indentation Errors in Python are, primarily caused because there are space or tab errors in your code. Since Python uses procedural language, you may experience this error if you have not placed the tabs/spaces correctly
df_job_da_details_filter_sort = df_job_da_details[df_job_da_details['Founded'] > 0].sort_values(['Company Name'], ascending=True)
df_job_da_details_filter_sort.head()
推荐阅读
- r - 如何从 R 代码中找到代数表达式?
- python - 如何在 pandas DataFrame.to_sql() 调用期间防止 SHOW FULL TABLES 查询
- c# - 如何使用 C# 检查 LocalSystem 帐户是否具有对特定本地目录的读/写权限?
- javascript - 仅获取 JavaScript 中对象数组中的特定值
- javascript - 尝试连接 Sendgrid API 时出错
- sql - 将列转换为行并在不同表中求和查询
- reactjs - 配置 Jsx-control-statement codesandbox.io
- swift - 三个按钮使用一个图像选择器,图像不更新
- c# - 运行 .exe 应用程序,该应用程序从 ac# 控制台应用程序中引用 2 个文件作为参数
- python - Python读取路径错误