Trong bài viết này, chúng tôi sẽ thảo luận về cách sắp xếp CSV theo cột bằng Python. Phương pháp 1: Sử dụng sort_values ()sort_values() Chúng tôi có thể lấy tên tiêu đề theo yêu cầu của chúng tôi, trục có thể là 0 hoặc 1, trong đó 0 có nghĩa là ‘hàng và‘ 1 có nghĩa là ‘cột. Tăng dần có thể là đúng/sai và nếu đúng, nó sẽ được sắp xếp theo thứ tự tăng dần, nếu sai, nó được sắp xếp theo thứ tự giảm dần. Cú pháp: dataFrame.sort_values (by, axis = 0, ascending = true, inplace = falseDataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)
Tệp CSV được sử dụng: Dưới đây là nhiều cách mô tả nhiều cách khác nhau để sắp xếp bộ dữ liệu CSV. Ví dụ 1: Sắp xếp bộ dữ liệu theo thứ tự giảm dần trên cơ sở tiền lươngSorting the dataset in descending order on the basis of salary Python3import pandas as pandasForSortingCSV
csvData = pandasForSortingCSV.read_csv( "sample.csv" )
print ( "\nBefore sorting:" )
print pandas as pandasForSortingCSV 2
pandas as pandasForSortingCSV 3pandas as pandasForSortingCSV 4pandas as pandasForSortingCSV 5
pandas as pandasForSortingCSV 6pandas as pandasForSortingCSV 7= pandas as pandasForSortingCSV 9csvData 0
pandas as pandasForSortingCSV 6csvData 2= csvData 4csvData 5pandas as pandasForSortingCSV 5
pandas as pandasForSortingCSV 6csvData 8= = 030
print ( = 4)
print pandas as pandasForSortingCSV 2
Output: Ví dụ 2: Sắp xếp bộ dữ liệu theo thứ tự mặc định (tăng dần) trên cơ sở tiền lương.Sorting the dataset in default (ascending) order on the basis of salary. Python3import pandas as pandasForSortingCSV
csvData = pandasForSortingCSV.read_csv( "sample.csv" )
print ( "\nBefore sorting:" )
pandas as pandasForSortingCSV 6csvData 8= = 030
"sample.csv" 1"sample.csv" 2pandas as pandasForSortingCSV 5
pandas as pandasForSortingCSV 6pandas as pandasForSortingCSV 7= pandas as pandasForSortingCSV 9csvData 0
pandas as pandasForSortingCSV 6csvData 8= = 0)
print ( = 4)
print pandas as pandasForSortingCSV 2
Output: Ví dụ 2: Sắp xếp bộ dữ liệu theo thứ tự mặc định (tăng dần) trên cơ sở tiền lương.Sorting the dataset on the basis of Name, Age and, Height in ascending order. Python3
import pandas as pandasForSortingCSV
csvData = pandasForSortingCSV.read_csv( "sample.csv" )
print ( "\nBefore sorting:" )
print pandas as pandasForSortingCSV 2
pandas as pandasForSortingCSV 3( 4( 5( 6( 5( 8pandas as pandasForSortingCSV 5
pandas as pandasForSortingCSV 6pandas as pandasForSortingCSV 7= pandas as pandasForSortingCSV 9csvData 0
pandas as pandasForSortingCSV 6csvData 2= csvData 4= 0( 5= 0( 5= 0pandas as pandasForSortingCSV 5
pandas as pandasForSortingCSV 6csvData 8= = 0)
print ( = 4)
print pandas as pandasForSortingCSV 2
Output: pandas as pandasForSortingCSV 6csvData 8= = 030Sorting the dataset on the basis of Salary in descending order and Age in ascending order.
Python3import pandas as pandasForSortingCSV
csvData = pandasForSortingCSV.read_csv( "sample.csv" )
print ( "\nBefore sorting:" )
print pandas as pandasForSortingCSV 2
pandas as pandasForSortingCSV 29"sample.csv" 2pandas as pandasForSortingCSV 31pandas as pandasForSortingCSV 32pandas as pandasForSortingCSV 33
pandas as pandasForSortingCSV 6pandas as pandasForSortingCSV 7= pandas as pandasForSortingCSV 9csvData 0
pandas as pandasForSortingCSV 6csvData 8= = 030
pandas as pandasForSortingCSV 6csvData 8= = 0)
print ( = 4)
print pandas as pandasForSortingCSV 2
Output: Ví dụ 2: Sắp xếp bộ dữ liệu theo thứ tự mặc định (tăng dần) trên cơ sở tiền lương.sorted() print pandas as pandasForSortingCSV 2sorted()method on the CSVmodule object. However, it can only sort CSV files based on only one column.
Syntax: Ví dụ 3: Sắp xếp bộ dữ liệu trên cơ sở tên, tuổi và chiều cao theo thứ tự tăng dần.
Dưới đây là nhiều cách mô tả nhiều cách khác nhau để sắp xếp bộ dữ liệu CSV. Ví dụ 1: Sắp xếp bộ dữ liệu theo thứ tự giảm dần trên cơ sở tiền lương Sorting the dataset in ascending order on the basis
of Age. Python3import pandas as pandasForSortingCSV
csvData = pandasForSortingCSV.read_csv( "sample.csv" )
pandas as pandasForSortingCSV 6csvData 8= = 030
print ( pandas as pandasForSortingCSV 80)
pandas as pandasForSortingCSV 82
Output: Ví dụ 2: Sắp xếp bộ dữ liệu theo thứ tự mặc định (tăng dần) trên cơ sở tiền lương. Sorting the dataset in
descending order on the basis of Age. Python3import pandas as pandasForSortingCSV
csvData = pandasForSortingCSV.read_csv( "sample.csv" )
pandas as pandasForSortingCSV 6csvData 8= = 030
print ( pandas as pandasForSortingCSV 80)
pandas as pandasForSortingCSV 82
Output:
Làm cách nào để tổ chức tệp CSV?
Sắp xếp một tệp CSV trong Excel.. Mở tệp CSV trong Excel .. Nhấn Ctrl + A .. Trong menu, chọn Dữ liệu> Sắp xếp .. Chọn hộp bên cạnh dữ liệu của tôi có tiêu đề .. Trong cột, chọn Cột bạn muốn sắp xếp danh sách của mình .. Chọn thứ tự bạn muốn sử dụng sắp xếp lại danh sách của mình ..
Làm cách nào để sắp xếp một tệp CSV trong Python bằng gấu trúc?
Sắp xếp dữ liệu trong một ... df = pd.read_csv ("c: /users/kennethcassel/homes.csv"). Sắp xếp_df = df.sort_values (by = ["giá"], tăng dần = false). Sắp xếp_df.to_csv ('homes_sorted.csv', index = false).
Panda có nhanh hơn CSV không?
Nhưng nó nhanh hơn để đọc dữ liệu nhanh hơn.Hãy xem làm thế nào.Trong bài viết này, chúng tôi sẽ đề cập: Đọc CSV mặc định của Pandas.... Đọc CSV với Pyarrow .. |