我的表:如何比較兩個不同列中熊貓的值?
A Country Code1 Code2
626349 US 640AD1237 407223
702747 NaN IO1062123 407255
824316 US NaN NaN
712947 US 00220221 870262123
278147 Canada 721AC31234 109123
278144 Canada NaN 7214234321
278142 Canada 72142QW134 109123AS12
這裏在上表中我需要檢查的國家和代碼。
我想用正確的或錯誤的,僞第5列:
If 'Country' == 'US' and (length(Code1) OR length(Code2) == 9):
Add values to 5th column as correct.
else:
Add values to 5th column as incorrect.
If 'Country' == 'Canada' and (length(Code1) OR length(Code2) == 10):
Add values to 5th column as correct.
else:
Add values to 5th column as incorrect.
如果沒有值存在無論是在國家或代碼比列信息不足。
我無法理解我該如何在熊貓中做到這一點。請幫忙。謝謝。
我試着先找到Code1和Code2的行的長度,並將其存儲在不同的df
中,但之後我無法比較不同的數據集作爲我需要做的。
Len1 = df.Code1.map(len)
Len2 = df.Code2.map(len)
LengthCode = pd.DataFrame({'Len_Code1': Len1,'Len_Code2': Len2})
請告訴我如何在單個數據框中儘可能地做到這一點的更好方法。
我想這
但越來越長,我就不能寫了許多國家。
請問你能告訴我什麼是錯的,或者我你是否投了我的問題 –
@Evan Wright你能幫我在這裏。 –