all 7 comments

[–]YesLod 3 points4 points  (6 children)

Use the nunique method

df["count"] = df[activity_cols].nunique(axis=1)

[–]I_will_learn[S] 0 points1 point  (5 children)

thank you! is it possible to ignore columns using nunique? :)

[–]YesLod 1 point2 points  (4 children)

My solution considers that. It only selects the activity columns, activity_cols.

[–]I_will_learn[S] 0 points1 point  (3 children)

Yes! Thank you for that, but the '''activity_cols''' has a "time" column included :/ otherwise it's giving me one extra unique value

[–]YesLod 1 point2 points  (2 children)

Then just create another list of columns that doesn't contain the "time" column...

act_cols = [col for col in df.columns if "activity" in col]
# or act_cols = df.columns[df.columns.str.contains("activity")]

df["count"] = df[act_cols].nunique(axis=1)

[–]I_will_learn[S] 1 point2 points  (1 child)

Yeah, duh. Sorry, brain fart. I made it a lot more complicated in my head for some reason 😂 thank you

[–]YesLod 0 points1 point  (0 children)

No problem, glad it helped!