简体   繁体   English

组合来自两个不同列的两个值并打印唯一值和唯一值计数

[英]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我有一个数据框,其中有两列,每列有 5 个值,我想组合两列中的所有值并打印所有唯一值并计算唯一值的数量

Example例子

Column 1 - 'Fruits 1' - has values these values [Apple, Orange, Banana, Grapes, Mango]第 1 列 - “水果 1” - 具有这些值 [Apple、Orange、Banana、Grapes、Mango]

Column 2 - 'Fruits 2' - has values these values [Apricot, Avocado, Blackberries, Grapes, Mango]第 2 列 - “水果 2” - 具有这些值 [杏、鳄梨、黑莓、葡萄、芒果]

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唯一值计数 = 8

Please can anybody help me with the code请任何人都可以帮助我的代码

You can use a set on the underlying numpy array:您可以在底层 numpy 数组上使用一set

set(df[['Fruits 1', 'Fruits 2']].values.ravel())

Output: Output:

{'Apple',
 'Apricot',
 'Avocado',
 'Banana',
 'Blackberries',
 'Grapes',
 'Mango',
 'Orange'}
length:长度:
len(set(df[['Fruits 1', 'Fruits 2']].values.ravel()))

Output: 8 Output: 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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