only keep uniques values of each column of a dataframe in python

You can use the apply method of a pandas dataframe along with the drop_duplicates method to keep only unique values of each column. Here's an example code snippet that demonstrates this:

main.py
import pandas as pd

# Sample dataframe
df = pd.DataFrame({
    'A': [1, 2, 3, 3],
    'B': ['x', 'y', 'z', 'y'],
    'C': [4.0, 5.0, 6.0, 5.0]
})

# Function to return unique values of a column
def get_unique(col):
    return col.drop_duplicates().tolist()

# Apply the function to each column of the dataframe
unique_df = df.apply(get_unique)
print(unique_df)
362 chars
17 lines

This will output the following dataframe, which contains the unique values of each column:

main.py
    A  B    C
0   1  x  4.0
1   2  y  5.0
2   3  z  6.0
56 chars
5 lines

gistlibby LogSnag