0

I'm looking to replace values in a Pandas column with their respective frequencies in the column.

I'm aware I can use value_counts to retrieve the frequency distribution for each value in the column. What I'm not sure on is how to replace every occurance of a value with its respective frequency.

An example dataframe:

   a      b  c
0  tiger  2  3
1  tiger  5  6
2  lion   8  9

Example output of df['a'].value_counts():

tiger    2
lion     1
Name: a, dtype: int64

Expected result when applied to column 'a':

   a  b  c
0  2  2  3
1  2  5  6
2  1  8  9
McGuile
  • 818
  • 1
  • 11
  • 29
  • 3
    try ```df.a = df.a.map(df.a.value_counts())``` – sammywemmy Apr 06 '20 at 09:41
  • 1
    @McGuile - One possible solution is use `replace` instead `map` for more columns – jezrael Apr 06 '20 at 09:52
  • @jezrael Any benefit to `replace` vs a loop over desired columns with `map`? – McGuile Apr 06 '20 at 10:00
  • replace is used for multiple columns, if want map it working only with one column/Series so necessary `apply(lambda x: x.map(df.a.value_counts()))`. But map is faster, only NaNs if no match – jezrael Apr 06 '20 at 10:01

0 Answers0