Skip to content
Advertisement

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?

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement