RMart
RMart

Reputation: 21

Sorting dataframe by two columns in Python

I'm getting the following error from the code below:

***File "<ipython-input-61-517e344a129d>", line 1
    df.sort_values(by "Script Count", "Drug Name"), axis=0, ascending=True, inplace=False, kind='quicksort',
                                   ^
SyntaxError: invalid syntax*** (the carrot is pointing to the second " following "Script Count")

Code:

import pandas as pd

import numpy as np

df = pd.read_csv('/Users/rmartin/Desktop/DE_Highmark.csv')

df.sort_values(by "Script Count", "Drug Name"), axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

I know this must be super simple, but I've driven myself crazy trying to figure it out. I'm trying to sort the dataframe based on the "Script Count" and "Drug Name" columns from a dataframe. The CSV was successfully imported as a dataframe, but the sort function is giving me trouble.

Upvotes: 2

Views: 496

Answers (1)

Marcel
Marcel

Reputation: 2794

Refering to the docs, the right syntax is:

 DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

so in your case, it should be:

df.sort_values(by=[ "Script Count", "Drug Name"], ascending=[True,True]) 

Upvotes: 2

Related Questions