NameError when attempting to sort pandas DataFrame column

2 weeks ago 12
ARTICLE AD BOX

like @ewokx said, trip_distance by itself means nothing to Python, it doesn't know that's a column inside your dataframe. You always have to reference it through the dataframe.

And also sorted() is not really the way to go with pandas, for that you use sort_values()

trip = df.sort_values("trip_distance", ascending=False)

here sort the whole dataframe by that column descending.

or if you only want that column i would go like this below:

trip = df["trip_distance"].sort_values(ascending=False)

just replace df with whatever your dataframe is actually called in your code and your good.

Jared McCarthy's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article