我知道問題名稱有點模糊。根據不同列值分配唯一值
我的目標是根據我的數據框中的2列+唯一值分配全局鍵列。
例如
CountryCode | Accident
AFG Car
AFG Bike
AFG Car
AFG Plane
USA Car
USA Bike
UK Car
讓車= 01,自行車= 02,平面= 03
我的願望全局密鑰格式爲[意外] [COUNTRYCODE] [UniqueValue]
獨特價值是一個類似的[計數] [國家代碼]
因此,如果事故=汽車和國家代碼= AFG,並且它是第一次出現,全局密鑰將是01AFG01
所需的數據幀是這樣的:
CountryCode | Accident | GlobalKey
AFG Car 01AFG01
AFG Bike 02AFG01
AFG Car 01AFG02
AFG Plane 01AFG03
USA Car 01USA01
USA Bike 01USA02
UK Car 01UK01
我曾嘗試循環追加事故次數和COUNTRYCODE一起
例如運行:
globalKey = []
for x in range(0,6):
string = df.iloc[x, 1]
string2 = df.iloc[x, 2]
if string2 == 'Car':
number = '01'
elif string2 == 'Bike':
number = '02'
elif string2 == 'Plane':
number = '03'
#Concat the number of accident and Country Code
subKey = number + string
#Append to the list
globalKey.append(subKey)
此代碼將根據我指定的值向我提供類似01AFG
,02AFG
的東西。但我想通過計算CountryCode
和Accident
相似時的發生次數來指定唯一值。
我被困在上面的代碼。我認爲應該有更好的方式在熊貓中使用地圖功能。
感謝您的幫助! 非常感謝!
不應該'AFG平面'行有'03AFG01'的'全局鍵'? –
和'USA Bike'是'02USA01'? – danio