Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to Sort CSV by a single column in Python ?
To sort a CSV file by a single column in Python, use the sort_values() method from Pandas. This method allows you to sort a DataFrame by specifying the column name and sort order.
Syntax
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, na_position='last')
Parameters
- by ? Column name to sort by
- axis ? 0 for rows, 1 for columns (default: 0)
- ascending ? True for ascending, False for descending (default: True)
- inplace ? Modify original DataFrame or return new one (default: False)
- na_position ? Where to place NaN values: 'first' or 'last' (default: 'last')
Example
Let's create a sample DataFrame with car sales data and sort it by different columns ?
import pandas as pd
# Create sample car sales data
data = {
'Car': ['BMW', 'Audi', 'Lexus', 'Jaguar', 'Mustang', 'Lamborghini'],
'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020',
'10/16/2020', '10/19/2020', '10/22/2020'],
'Reg_Price': [1000, 750, 1250, 1500, 1100, 1000]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Original DataFrame:
Car Date_of_Purchase Reg_Price
0 BMW 10/10/2020 1000
1 Audi 10/12/2020 750
2 Lexus 10/17/2020 1250
3 Jaguar 10/16/2020 1500
4 Mustang 10/19/2020 1100
5 Lamborghini 10/22/2020 1000
Sorting by Car Names
Sort the DataFrame alphabetically by the 'Car' column ?
import pandas as pd
data = {
'Car': ['BMW', 'Audi', 'Lexus', 'Jaguar', 'Mustang', 'Lamborghini'],
'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020',
'10/16/2020', '10/19/2020', '10/22/2020'],
'Reg_Price': [1000, 750, 1250, 1500, 1100, 1000]
}
df = pd.DataFrame(data)
# Sort by Car column (ascending)
sorted_by_car = df.sort_values("Car")
print("Sorted by Car Names:")
print(sorted_by_car)
Sorted by Car Names:
Car Date_of_Purchase Reg_Price
1 Audi 10/12/2020 750
0 BMW 10/10/2020 1000
3 Jaguar 10/16/2020 1500
5 Lamborghini 10/22/2020 1000
2 Lexus 10/17/2020 1250
4 Mustang 10/19/2020 1100
Sorting by Price
Sort the DataFrame by registration price in ascending order ?
import pandas as pd
data = {
'Car': ['BMW', 'Audi', 'Lexus', 'Jaguar', 'Mustang', 'Lamborghini'],
'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020',
'10/16/2020', '10/19/2020', '10/22/2020'],
'Reg_Price': [1000, 750, 1250, 1500, 1100, 1000]
}
df = pd.DataFrame(data)
# Sort by Registration Price
sorted_by_price = df.sort_values("Reg_Price")
print("Sorted by Registration Price:")
print(sorted_by_price)
Sorted by Registration Price:
Car Date_of_Purchase Reg_Price
1 Audi 10/12/2020 750
0 BMW 10/10/2020 1000
5 Lamborghini 10/22/2020 1000
4 Mustang 10/19/2020 1100
2 Lexus 10/17/2020 1250
3 Jaguar 10/16/2020 1500
Sorting in Descending Order
To sort in descending order, set ascending=False ?
import pandas as pd
data = {
'Car': ['BMW', 'Audi', 'Lexus', 'Jaguar', 'Mustang', 'Lamborghini'],
'Reg_Price': [1000, 750, 1250, 1500, 1100, 1000]
}
df = pd.DataFrame(data)
# Sort by price in descending order
sorted_desc = df.sort_values("Reg_Price", ascending=False)
print("Sorted by Price (Descending):")
print(sorted_desc)
Sorted by Price (Descending):
Car Reg_Price
3 Jaguar 1500
2 Lexus 1250
4 Mustang 1100
0 BMW 1000
5 Lamborghini 1000
1 Audi 750
Conclusion
Use sort_values() to sort a DataFrame by any column. Set ascending=False for descending order, and use inplace=True to modify the original DataFrame directly.
Advertisements
