Most Efficient Way To Return Column Name In A Pandas Df February 01, 2024 Post a Comment I have a pandas df that contains 4 different columns. For every row theres a value thats of importance. I want to return the Column name where that value is displayed. So for the dSolution 1: Use DataFrame.dot:df.astype(bool).dot(df.columns).str.cat(sep=',') CopyOr,','.join(df.astype(bool).dot(df.columns)) Copy'A,C,B,A'CopyOr, as a list:df.astype(bool).dot(df.columns).tolist() ['A', 'C', 'B', 'A']Copy...or a Series:df.astype(bool).dot(df.columns) 0A1C2B3Adtype: objectCopy Share Post a Comment for "Most Efficient Way To Return Column Name In A Pandas Df"
Post a Comment for "Most Efficient Way To Return Column Name In A Pandas Df"