I have three datasets (final_NN, ppt_code, herd_id), and I wish to add a new column called MapValue in final_NN dataframe, and the value to be added can be retrieved from the other two dataframes, the rule is in the bottom after codes.
import pandas as pd
final_NN = pd.DataFrame({
"number": [123, 456, "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown"],
"ID": ["", "", "", "", "", "", "", "", 799, 813],
"code": ["", "", "AA", "AA", "BB", "BB", "BB", "CC", "", ""]
})
ppt_code = pd.DataFrame({
"code": ["AA", "AA", "BB", "BB", "CC"],
"number": [11, 11, 22, 22, 33]
})
herd_id = pd.DataFrame({
"ID": [799, 813],
"number": [678, 789]
})
new_column = pd.Series([])
for i in range(len(final_NN)):
if final_NN["number"][i] != "" and final_NN["number"][i] != "Unknown":
new_column[i] = final_NN['number'][i]
elif final_NN["code"][i] != "":
for p in range(len(ppt_code)):
if ppt_code["code"][p] == final_NN["code"][i]:
new_column[i] = ppt_code["number"][p]
elif final_NN["ID"][i] != "":
for h in range(len(herd_id)):
if herd_id["ID"][h] == final_NN["ID"][i]:
new_column[i] = herd_id["number"][h]
else:
new_column[i] = ""
final_NN.insert(3, "MapValue", new_column)
print(final_NN)
final_NN:
number ID code
0 123
1 456
2 Unknown AA
3 Unknown AA
4 Unknown BB
5 Unknown BB
6 Unknown BB
7 Unknown CC
8 Unknown 799
9 Unknown 813
ppt_code:
code number
0 AA 11
1 AA 11
2 BB 22
3 BB 22
4 CC 33
herd_id:
ID number
0 799 678
1 813 789
Expected output:
number ID code MapValue
0 123 123
1 456 456
2 Unknown AA 11
3 Unknown AA 11
4 Unknown BB 22
5 Unknown BB 22
6 Unknown BB 22
7 Unknown CC 33
8 Unknown 799 678
9 Unknown 813 789
The rules is:
- if
numberin final_NN is notUnknown,MapValue=numberinfinal_NN; - if
numberin final_NN isUnknownbutcodeinfinal_NNis not Null, then search the ppt_code dataframe, and use thecodeand its corresponding "number" to map and fill in the "MapValue" infinal_NN; - if both
numberandcodeinfinal_NNareUnknownand null respectively, butIDinfinal_NNis not Null, then searchherd_iddataframe, and use theIDand its correspondingnumberto fill in theMapValuein the first dataframe. I applied a loop through the dataframe which is a slow way to achieve this, as above. But I understand there could be a faster way to do this. Just wondering would anyone help me to have a fast and easier way to achieve the same result?