PythonでCSVを単一の列で並べ替える方法は?
CSVを単一の列で並べ替えるには、sort_values()メソッドを使用します。 sort_values()メソッドで並べ替える列を設定します。
まず、DataFrameを使用してCSVファイル「SalesRecords.csv」を読みましょう-
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
単一の列「車」に従って並べ替える-
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 # DataFrame to read our input CS file dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv") print("\nInput CSV file = \n", dataFrame) # sorting according to Car column dataFrame.sort_values("Car", axis=0, ascending=True,inplace=True, na_position='first') print("\nSorted CSV file (according to Car Names) = \n", dataFrame) # sorting according to Reg_Price column 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
-
複数のCSVファイルを単一のPandasデータフレームにマージする方法は?
複数のCSVファイルを単一のPandasデータフレームにマージするには、read_csvを使用します。まず、必要なパンダライブラリをインポートします。ここ。 pdをエイリアスとして設定しました- import pandas as pd さて、以下が私たちのCSVファイルだとしましょう- Sales1.csv Sales2.csv パスを文字列として設定しました。両方のファイルがデスクトップにあります- file1 = "C:\\Users\\amit_\\Desktop\\sales1.csv" file2 = "C:\\Users
-
Python Pandasでデータフレーム列の値をX軸ラベルとして設定するにはどうすればよいですか?
Python Pandasでデータフレーム列の値をX軸ラベルとして設定するには、 xticksを使用できます。 plot()の引数で メソッド。 ステップ 図のサイズを設定し、サブプロット間およびサブプロットの周囲のパディングを調整します。 column1キーのパンダを使用してデータフレームを作成します 。 plot()を使用してPandasデータフレームをプロットします X軸の列としてcolumn1を使用するメソッド。 図を表示するには、 show()を使用します メソッド。 例 import pandas as pd from matplotlib impo