I have a data frame where I have two columns and each column has 5 values each and I want to combine all values from the two columns and print all unique values and count number of unique values
Example
Column 1 - 'Fruits 1' - has values these values [Apple, Orange, Banana, Grapes, Mango]
Column 2 - 'Fruits 2' - has values these values [Apricot, Avocado, Blackberries, Grapes, Mango]
Now I want to combine values from both the columns and print all unique values and also want count of unique when both are combined
expected result = [Apple, Orange, Banana, Grapes, Mango, Apricot, Avocado, Blackberries]
Unique value count = 8
Please can anybody help me with the code
CodePudding user response:
You can use a set
on the underlying numpy array:
set(df[['Fruits 1', 'Fruits 2']].values.ravel())
Output:
{'Apple',
'Apricot',
'Avocado',
'Banana',
'Blackberries',
'Grapes',
'Mango',
'Orange'}
length:
len(set(df[['Fruits 1', 'Fruits 2']].values.ravel()))
Output: 8