Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign count of unique values to the records in a data frame in python

Tags:

python

pandas

I have a data frame like this:

IP_address
   IP1
   IP1
   IP1
   IP4
   IP4
   IP4
   IP4
   IP4
   IP7
   IP7
   IP7

I would like to take count of unique values in this column and add the count as a variable by itself. At the end, it should look like this:

IP_address  IP_address_Count
   IP1               3
   IP1               3
   IP1               3
   IP4               5
   IP4               5
   IP4               5
   IP4               5
   IP4               5
   IP7               3
   IP7               3
   IP7               3

I am able to take the unique values of the column using the below code:

unique_ip_address_count = (df_c_train.drop_duplicates().IP_address.value_counts()).to_dict()

However, I am not sure how to match these in a loop in python so that i can get the desired results in python. Any sort of help is much appreciated.

I am not able to find a equivalent answer in stackoverflow. If there is anything please direct me there. Thank you.

like image 847
Doubt Dhanabalu Avatar asked Sep 20 '17 20:09

Doubt Dhanabalu


People also ask

How do you count unique values in a DataFrame in Python?

To count unique values in the pandas dataframe column use Series. unique() function and then call the size to get the count. Series.

How do you count unique values in a data set?

read_csv() function in which pass the path and name of the dataset. Select the column in which you want to check or count the unique values. For finding unique values we are using unique() function provided by pandas and stored it in a variable, let named as 'unique_values'.


1 Answers

You can use value_counts() with map

df['count'] = df['IP_address'].map(df['IP_address'].value_counts())


    IP_address  count
0   IP1         3
1   IP1         3
2   IP1         3
3   IP4         5
4   IP4         5
5   IP4         5
6   IP4         5
7   IP4         5
8   IP7         3
9   IP7         3
10  IP7         3
like image 103
Vaishali Avatar answered Oct 24 '22 03:10

Vaishali