【问题标题】:How to check frequency of every unique value from pandas data-frame?如何检查熊猫数据框中每个唯一值的频率?
【发布时间】:2019-11-14 07:07:26
【问题描述】:

如果我有一个 2000 年的数据框,假设品牌有 142 个唯一值,我想计算从 1 到 142 的每个唯一值的频率。值应该动态变化。

brand=clothes_z.brand_name
brand.describe(include="all")
unique_brand=brand.unique()
brand.describe(include="all"),unique_brand

输出:

(count       2613
unique      142
 top       Mango
 freq         54
 Name: brand_name, dtype: object,
array(['Jack & Jones', 'TOM TAILOR DENIM', 'YOURTURN', 'Tommy Jeans',
        'Alessandro Zavetti', 'adidas Originals', 'Volcom', 'Pier One',
        'Superdry', 'G-Star', 'SIKSILK', 'Tommy Hilfiger', 'Karl Kani',
        'Alpha Industries', 'Farah', 'Nike Sportswear',
        'Calvin Klein Jeans', 'Champion', 'Hollister Co.', 'PULL&BEAR',
        'Nike Performance', 'Even&Odd', 'Stradivarius', 'Mango',
        'Champion Reverse Weave', 'Massimo Dutti', 'Selected Femme Petite',
        'NAF NAF', 'YAS', 'New Look', 'Missguided', 'Miss Selfridge',
        'Topshop', 'Miss Selfridge Petite', 'Guess', 'Esprit Collection',
        'Vero Moda', 'ONLY Petite', 'Selected Femme', 'ONLY', 'Dr.Denim',
        'Bershka', 'Vero Moda Petite', 'PULL & BEAR', 'New Look Petite',
        'JDY', 'Even & Odd', 'Vila', 'Lacoste', 'PS Paul Smith',
        'Redefined Rebel', 'Selected Homme', 'BOSS', 'Brave Soul', 'Mind',
        'Scotch & Soda', 'Only & Sons', 'The North Face',
        'Polo Ralph Lauren', 'Gym King', 'Selected Woman', 'Rich & Royal',
        'Rooms', 'Glamorous', 'Club L London', 'Zalando Essentials',
        'edc by Esprit', 'OYSHO', 'Oasis', 'Gina Tricot',
        'Glamorous Petite', 'Cortefiel', 'Missguided Petite',
        'Missguided Tall', 'River Island', 'INDICODE JEANS',
        'Kings Will Dream', 'Topman', 'Esprit', 'Diesel', 'Key Largo',
        'Mennace', 'Lee', "Levi's®", 'adidas Performance', 'jordan',
        'Jack & Jones PREMIUM', 'They', 'Springfield', 'Benetton', 'Fila',
        'Replay', 'Original Penguin', 'Kronstadt', 'Vans', 'Jordan',
        'Apart', 'New look', 'River island', 'Freequent', 'Mads Nørgaard',
        '4th & Reckless', 'Morgan', 'Honey punch', 'Anna Field Petite',
        'Noisy may', 'Pepe Jeans', 'Mavi', 'mint & berry', 'KIOMI', 'mbyM',
        'Escada Sport', 'Lost Ink', 'More & More', 'Coffee', 'GANT',
        'TWINTIP', 'MAMALICIOUS', 'Noisy May', 'Pieces', 'Rest',
        'Anna Field', 'Pinko', 'Forever New', 'ICHI', 'Seafolly', 'Object',
        'Freya', 'Wrangler', 'Cream', 'LTB', 'G-star', 'Dorothy Perkins',
        'Carhartt WIP', 'Betty & Co', 'GAP', 'ONLY Tall', 'Next', 'HUGO',
        'Violet by Mango', 'WEEKEND MaxMara', 'French Connection'],
       dtype=object))

因为它只显示芒果“54”的频率,因为它是最高频率,我想要每个值频率,比如Jack & JonesTOM TAILOR DENIMYOURTURN 的频率等等......和值应该动态变化。

【问题讨论】:

    标签: python arrays pandas dataframe


    【解决方案1】:

    你可以这样做,

    clothes_z.brand_name.value_counts()
    

    这将列出唯一值,并为您提供该熊猫系列中每个元素的频率。

    【讨论】:

    • 是的!伙计,这也是一种简单的方法..我们可以将这个系列转换为数据框并绘制可视化
    【解决方案2】:
    from collections import Counter
    
    
    ll = [...your list of brands...]
    c = Counter(ll)
    # you can do whatever you want with your counted values
    df = pd.DataFrame.from_dict(c, orient='index', columns=['counted'])
    

    【讨论】:

    • 它返回 collections.Counter 以及如何将其转换为数据帧表
    • 只需了解如何进行计数器 -> 数据框。值得注意的是,集合是 python 中值得了解的强大模块。请查看我的更新答案。
    • 谢谢你让我的生活变得轻松......!我怎样才能给你信用..?
    猜你喜欢
    • 1970-01-01
    • 2020-12-08
    • 2019-11-22
    • 2015-01-30
    • 2021-01-25
    • 2019-11-23
    • 2016-08-03
    • 1970-01-01
    相关资源
    最近更新 更多