I want to calculate the frequencies of values of a dataframe column in a column from another dataframe. Right now, I have the code as below:
df2["freq"] = df1[["col1"]].groupby(df2["col2"])["col1"].transform('count')
But it is giving freq of 1.0 for all the values in df2["col2"], even for those values that don't exist in df1["col1"].
df1:
col1
0 636
1 636
2 801
3 802
df2:
col2
0 636
1 734
2 801
3 803
df2 after adding freq column:
col2 freq
0 636 1.0
1 734 1.0
2 801 1.0
3 803 1.0
What I actually want:
col2 freq
0 636 2
1 734 0
2 801 1
3 803 0
I am new to pandas, so I am not getting what I am doing wrong. Any help is appreciated! Thanks!
your_column.value_counts()