Counting the numer of occurrences of a certain value in a column

Viewed 71

I have a DataFrame in the form of this picture:df1

I am trying to create a new column that would have a count of the times that each individual Administrative port showed in the dataset.df2

An idea I tried was to create a new DataFrame with each unique port as a row. Then iterate over each row and feed the value of each row into .value_counts as so.

for index, row in df2.iterrows():
    df1['count'] = df1.value_counts().df2.row['Administrative port']
1 Answers

Use Series.map:

df1['count'] = df1['Administrative port'].map(df1['Administrative port'].value_counts())
Related