I have the following problem:
In case I want to get the columns of a dataframe which have all same strings I use the code that follows:
Let's create the dataframe first: df_example1 = pd.DataFrame({'A':[1,2,3],'B':[1,2,3]})
. Now let's look for the columns that have exactly the same strings: [(i, j) for i,j in combinations(df_example1, 2) if df_example1[i].equals(df_example1[j])]
The code returns the tuple [('A', 'B')]
My problem is: In case I want to get a tuple of columns which have ONLY two of the strings the same what code should I use? Let's say that my dataframe is the following: df_example2 = pd.DataFrame({'A':[2,3,4],'B':[1,2,3]})
and it should return the tuple [('A', 'B')].
Thank you in advance :)
source https://stackoverflow.com/questions/71145913/get-a-list-of-tuples-with-the-columns-that-have-only-two-same-strings
Comments
Post a Comment