Get common values in multiple columns of a dataframe?

Asked

Viewed 755 times

1

I have a table with numerical values and would like to take the values in common in all columns (intersection) and the unique values of each using pandas (Python).

    Position_a  Position_b  Position_c  Position_d  Position_e  Position_f
0   45794939.0  45794939.0  45794939    45794939.0  45794939    45794939.0
1   45794975.0  45794975.0  45794975    45794975.0  45794975    45794975.0
2   45794983.0  45794983.0  45794983    45794983.0  45794983    45794983.0
3   45794988.0  45794988.0  45794988    45794988.0  45794988    45794988.0
4   45795006.0  45795006.0  45795006    45795006.0  45795006    45795006.0
  • 1

    Hello Leandro, welcome to Stackoverflow in English, could you translate your question? If possible do a tour to get to know the site better.

1 answer

0


Your example is a little strange, since all the rows are present in all columns, but maybe the following method to count the occurrences will help you.

    import pandas as pd
    import numpy as np

    data = {'Position_a': [45794975, 45794983, 45794988, 45795006, np.NaN],
            'Position_b': [45794939, 45794975, 45794983, 45794988, 45795006],
            'Position_c': [45794939, 45794975, 45794983, 45794988, 45795006],
            'Position_d': [45794939, 45794975, 45794983, 45794988,np.NaN],
            'Position_e': [45794939, 45794975, 45794983, 45795006, np.NaN],
            'Position_f': [45794939, 45794975, 45794983, 45794988, 45795006]
            }

    df = pd.DataFrame.from_dict(data)

    print(df)

    tidydf = pd.melt(df)

    tidydf.pivot_table(index = ['value'], columns = ['variable'], aggfunc = len, fill_value = 0)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.