Issue
I have the following df
:
df = pd.DataFrame(
[
[["John Muller"], "person", [8866155845]],
[["Innovation Division"], "company", np.nan],
[["Carol Sway"], "person", [8866155845]],
],
columns=["name", "kind", "phone"],
)
# Out:
# name kind phone
# 0 [John Muller] person [8866155845]
# 1 [Innovation Division] company NaN
# 2 [Carol Sway] person [8866155845]
and I want to find duplicates of a phone number. But the objects in df
are lists, so using:
df.duplicated('phone')
will generate the error:
TypeError: unhashable type: 'list'
Solution
You can also use applymap
function which is quite handy to solve this problem:
# get duplicated row
df2 = df[df.applymap(lambda x: x[0] if isinstance(x, list) else x).duplicated('phone')]
print(df2)
name kind phone
2 [Carol Sway] person [8866155845]
Answered By - YOLO
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.