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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With