Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique values of two columns for pandas dataframe [duplicate]

Suppose I have pandas data frame with 2 columns:

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:

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?

like image 728
curious_one Avatar asked Jul 04 '17 13:07

curious_one


People also ask

How do I get unique values of two columns in pandas?

Pandas series aka columns has a unique() method that filters out only unique values from a column. The first output shows only unique FirstNames. We can extend this method using pandas concat() method and concat all the desired columns into 1 single column and then find the unique of the resultant column.

How can I get unique values of a column in pandas with Count?

To get a count of unique values in a column use pandas, first use Series. unique() function to get unique values from column by removing duplidate values and then call the size to get the count. unique() function returns a ndarray with unique value in order of appearance and the results are not sorted.

How do I get unique values from two columns?

Find unique/duplicate values between two columns with formula. The following formula can also help you to find the unique values, please do as this: In a blank cell B2, enter this formula =IF(ISNA(VLOOKUP(A2,$C$2:$C$13,1,FALSE)),"Yes",""), and then drag this cell's AutoFill Handle to the cell B15.


1 Answers

You need 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 
like image 77
jezrael Avatar answered Oct 07 '22 22:10

jezrael