I am working with a pandas DataFrame and trying to sort a numeric column (trip_distance) in descending order.
However, I receive a NameError: name 'trip_distance' is not defined when using:
trip = sorted(trip_distance)
print(trip)
The column exists inside my DataFrame (e.g., df).
I would like clarity on:
-
Why this
NameErroroccurs -
The correct way to sort a DataFrame column in descending order
-
When to use
sorted()versusDataFrame.sort_values()
Any guidance on best practices for sorting columns in pandas would be appreciated.
