CSV को एकल कॉलम द्वारा सॉर्ट करने के लिए, sort_values() मेथड का उपयोग करें। sort_values() मेथड में उस कॉलम को सेट करें जिसका उपयोग करके आप सॉर्ट करना चाहते हैं।
पहले, आइए हमारे CSV फ़ाइल "SalesRecords.csv" को डेटाफ्रेम के साथ पढ़ें:
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
एकल कॉलम "Car" के अनुसार सॉर्ट करें:
dataFrame.sort_values("Car", axis=0, ascending=True, inplace=True, na_position='first')
अगला, एकल कॉलम "Reg_Price" के अनुसार सॉर्ट करें:
dataFrame.sort_values("Reg_Price", axis=0, ascending=True, inplace=True, na_position='first')
उदाहरण
निम्नलिखित कोड है:
import pandas as pd
# हमारी इनपुट CSV फ़ाइल पढ़ने के लिए डेटाफ्रेम
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
print("\nInput CSV file = \n", dataFrame)
# Car कॉलम के अनुसार सॉर्ट करें
dataFrame.sort_values("Car", axis=0, ascending=True, inplace=True, na_position='first')
print("\nSorted CSV file (according to Car Names) = \n", dataFrame)
# Reg_Price कॉलम के अनुसार सॉर्ट करें
dataFrame.sort_values("Reg_Price", axis=0, ascending=True, inplace=True, na_position='first')
print("\nSorted CSV file (according to Registration Price) = \n", dataFrame)
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
Input CSV file = 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 Sorted CSV file (according to 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 Sorted CSV file (according to 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