Python Pandas - 返回值中刪除重複值,但保留第一個值
要返回值中刪除重複值,但保留第一個值,請使用 index.drop_duplicates() 方法。使用 keep 引數,值設為 first。
首先,匯入所需的庫 −
import pandas as pd
建立具有某些重複值的索引 −
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
顯示索引 −
print("Pandas Index with duplicates...\n",index)
返回值中刪除重複值。值設為 "first" 的 "keep" 引數保留每組重複條目的第一次出現 −
index.drop_duplicates(keep='first')
示例
以下是程式碼 −
import pandas as pd # Creating the index with some duplicates index = pd.Index(['Car','Bike','Airplane','Ship','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 # The "keep" parameter with value "first" keeps the first occurrence for each set of duplicated entries print("\nIndex with duplicate values removed (keeping the first occurrence)...\n",index.drop_duplicates(keep='first'))
輸出
這將生成以下程式碼 −
Pandas Index with duplicates... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane'], dtype='object') The dtype object... object Get the bytes... 40 Get the dimensions... 1 Index with duplicate values removed (keeping the first occurrence)... Index(['Car', 'Bike', 'Airplane', 'Ship'], dtype='object')
廣告