Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonPandas-重複する値が削除されたインデックスを返す


重複する値を削除してインデックスを返すには、 index.drop_duplicates()を使用します メソッド。

まず、必要なライブラリをインポートします-

import pandas as pd

いくつかの重複を含むインデックスの作成-

index = pd.Index(['Car','Bike','Truck','Car','Airplane'])

インデックスを表示-

print("Pandas Index with duplicates...\n",index)

重複する値を削除してインデックスを返します。デフォルトでは、重複する値の最初の出現は削除されたままになります-

print("\nIndex with duplicate values removed...\n",index.drop_duplicates())

以下はコードです-

import pandas as pd

# Creating the index with some duplicates
index = pd.Index(['Car','Bike','Truck','Car','Airplane'])

# Display the index
print("Pandas Index with duplicates...\n",index)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# get the bytes in the data
print("\nGet the bytes...\n",index.nbytes)

# get the dimensions of the data
print("\nGet the dimensions...\n",index.ndim)

# Return Index with duplicate values removed
# By default it keeps the first occurrence of the duplicate value removed
print("\nIndex with duplicate values removed...\n",index.drop_duplicates())

出力

これにより、次のコードが生成されます-

Pandas Index with duplicates...
Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'], dtype='object')

The dtype object...
object

Get the bytes...
40

Get the dimensions...
1

Index with duplicate values removed...
Index(['Car', 'Bike', 'Truck', 'Airplane'], dtype='object')

  1. Python-Pandasインデックスの最大値を返します

    パンダインデックスの最大値を返すには、 index.max()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成 index = pd.Index([10, 20, 70, 40, 90, 50, 25, 30]) パンダのインデックスを表示する- print("Pandas Index...\n",index) 最大値を取得- print("\nMaximum value..\n", index.max()) 例 以下はコードです- import pandas a

  2. Python-Pandasインデックスの最小値を返します

    パンダインデックスの最小値を返すには、 index.min()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成- index = pd.Index([10.5, 20.4, 40.5, 25.6, 5.7, 6.8, 30.8, 50.2]) パンダのインデックスを表示する- print("Pandas Index...\n",index) 最小値を取得- print("\nMinimum value..\n", index.min()) 例 以下はコードです-