I know there have been several questions asked regarding using nunique() with groupby, but I get a different error message. The following code works fine:
d = {'PDOS': [1, 1, 2, 2], 'CPT' : ["123", "123", "456", "456"], 'BC': ["A", "A", "A", "B"], 'Other': [1,2,3,4]}
df = pd.DataFrame(data = d)
df = df.merge(df[["PDOS", "CPT", "BC"]].groupby(["PDOS", "CPT"]).count(), how = "left", on = ["PDOS", "CPT"])
and I get a new column ("BC_y") which gives me the number of BC rows per each group.
however, if I change the last line to use nunique() as follows:
df = df.merge(df[["PDOS", "CPT", "BC"]].groupby(["PDOS", "CPT"]).nunique(), how = "left", on = ["PDOS", "CPT"])
I get the error message:
You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
I think the reason may be when using nunique() the columns "PDOS" and "CPT" show up as the index as well as columns.
How can I get the number of unique values instead of the number of rows per group?