Python pandas 数据框的两列的唯一值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44906754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:36:10  来源:igfitidea点击:

Unique values of two columns for pandas dataframe

pythonpandasdataframeunique

提问by curious_one

Suppose I have pandas data frame with 2 columns:

假设我有 2 列的 Pandas 数据框:

df: Col1  Col2
      1     1
      1     2
      1     2
      1     2
      3     4
      3     4

Then I want to keep only the unique couple values (col1, col2) of these two columns and give their frequncy:

然后我只想保留这两列的唯一值(col1,col2)并给出它们的频率:

df2: Col1  Col2  Freq
      1     1     1
      1     2     3
      3     4     2

I think to use df['Col1', 'Col2'].value_counts()but it works only for one column. Does it exist a function to deal with many columns?

我想使用df['Col1', 'Col2'].value_counts()但它只适用于一列。是否存在处理多列的函数?

回答by jezrael

You need groupby+ size+ Series.reset_index:

你需要groupby+ size+ Series.reset_index

df = df.groupby(['Col1', 'Col2']).size().reset_index(name='Freq')
print (df)
   Col1  Col2  Freq
0     1     1     1
1     1     2     3
2     3     4     2

回答by Quickbeam2k1

You could try

你可以试试

df.groupby(['Col1', 'Col2']).size()

for a different visual output in comparison to jez's answer, you can extend that solution with

与 jez 的答案相比,对于不同的视觉输出,您可以扩展该解决方案

pd.DataFrame(df.groupby(['Col1', 'Col2']).size().rename('Freq'))

gives

           Freq
Col1 Col2      
1    1        1
     2        3
3    4        2