''Series' object has no attribute 'values_counts'

When I try to apply the values_count() method to series within a function, I am told that 'Series' object has no attribute 'values_counts'.

def replace_1_occ_feat(col_list, df):
    for col in col_list:
        feat_1_occ = df[col].values_counts()[df[col].values_counts() == 1].index 
        feat_means = df[col].groupby(col)['SalePrice'].mean()
        feat_means_no_1_occ = feat_means.iloc[feat_means.difference(feat_1_occ),:]
        for feat in feat_1_occ:
            # Find the closest mean SalePrice
            replacement = (feat_means_no_1_occ - feat_means.iloc[feat,:]).idxmin()
            df.col.replace(feat, replacement, inplace = True)

However when running df.column.values_count() outside a function it works.

The problem occurs on the first line when the values_counts() methods is used. I checked the pandas version it's 0.23.0.



Solution 1:[1]

The function is value_counts(). Note only count is plural.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kyle