[英]How to find primary key in “DataFrame” object?
如果它是唯一鍵,則行軸的長度應等於唯一值的計數。
x = df.shape[0]
l = df['Phone'].drop_duplicates().shape[0]
assert x == l
根據您的完整問題陳述,您很可能需要同時使用姓名和電話來創建唯一索引。
x = df.shape[0]
l = df[['Name', 'Phone']].drop_duplicates().shape[0]
assert x == l
set_index()
有一個參數verify_integrity
如果您有重復的鍵,則會引發 ValueError :
# has duplicate phone number; raises value error
df = pd.DataFrame({'phones': [123, 234, 345, 345],
'names': ['A', 'B', 'C', 'D']})
df = df.set_index('phones',
verify_integrity=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.