python - count how many unique in a column 1

python – count how many unique in a column

df['var_1'].nunique()   # How many unque values are present in a variable

Here is what the above code is Doing:
1. We are creating a new dataframe called ‘df_new’ which is a copy of the original dataframe ‘df’.
2. We are creating a new column called ‘var_1_new’ in the new dataframe ‘df_new’.
3. We are replacing the values in the new column ‘var_1_new’ with the values in the column ‘var_1’ of the original dataframe ‘df’.
4. We are replacing the values in the column ‘var_1’ of the original dataframe ‘df’ with the values in the new column ‘var_1_new’.
5. We are dropping the new column ‘var_1_new’ from the new dataframe ‘df_new’.

Similar Posts