简体   繁体   中英

combining two values from two different columns and print unique values and count of unique values

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM