楼主: sharlen_deng
20059 7

[问答] python dataframe中各元素出现次数统计及画直方图 [推广有奖]

  • 0关注
  • 0粉丝

初中生

9%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
59 点
帖子
5
精华
0
在线时间
15 小时
注册时间
2018-4-10
最后登录
2018-10-12

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
各位大神求问,现在我使用的是R语言的数据集groceries,使用python做关联规则。将数据集命名为a,有32列,我想要算出dataframe里面各元素都出现了多少次,应该用什么命令?如果我print(Counter(a[0]))的命令可以算出第0列中元素出现了多少次,但是我打print(Counter(a[0:31]))他却输入的是Counter({0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 27: 1, 28: 1, 29: 1, 30: 1, 31: 1})
请问需要怎么解决?
顺便一问再用各元素的总数画出直方图应该怎么操作?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:关联规则 数据集 直方图 联规则

沙发
sharlen_deng 发表于 2018-5-27 16:07:59 |只看作者 |坛友微信交流群
比如:
print(Counter(a[0]))
Counter({'sausage': 825, 'whole milk': 717, 'frankfurter': 580, 'tropical fruit': 482, 'other vegetables': 460, 'citrus fruit': 453, 'pork': 385, 'rolls/buns': 336, 'chicken': 311, 'canned beer': 310, 'beef': 304, 'soda': 294, 'root vegetables': 288, 'pip fruit': 277, 'yogurt': 225, 'ham': 186, 'bottled beer': 178, 'bottled water': 170, 'meat': 170, 'hamburger meat': 167, 'pastry': 132, 'berries': 121, 'curd': 91, 'ice cream': 82, 'coffee': 80, 'beverages': 80, 'whipped/sour cream': 77, 'butter': 76, 'dessert': 71, 'UHT-milk': 70, 'onions': 70, 'grapes': 69, 'brown bread': 57, 'newspapers': 56, 'domestic eggs': 52, 'frozen meals': 51, 'finished products': 50, 'misc. beverages': 49, 'turkey': 49, 'shopping bags': 48, 'chocolate': 46, 'butter milk': 43, 'fruit/vegetable juice': 39, 'salty snack': 39, 'liver loaf': 38, 'cream cheese': 35, 'frozen vegetables': 35, 'specialty chocolate': 32, 'packaged fruit/vegetables': 30, 'waffles': 30, 'herbs': 29, 'oil': 29, 'photo/film': 28, 'white bread': 28, 'chewing gum': 26, 'white wine': 25, 'margarine': 25, 'condensed milk': 24, 'specialty bar': 23, 'pet care': 23, 'cat food': 23, 'sugar': 22, 'fish': 19, 'hard cheese': 19, 'napkins': 19, 'semi-finished bread': 18, 'long life bakery product': 18, 'processed cheese': 16, 'frozen fish': 16, 'hygiene articles': 16, 'detergent': 15, 'liquor': 15, 'sliced cheese': 15, 'nuts/prunes': 15, 'candy': 14, 'spread cheese': 14, 'red/blush wine': 13, 'zwieback': 13, 'pasta': 12, 'potted plants': 11, 'sparkling wine': 11, 'frozen dessert': 11, 'organic sausage': 11, 'dog food': 10, 'baking powder': 10, 'dishes': 10, 'seasonal products': 10, 'curd cheese': 9, 'frozen potato products': 9, 'soft cheese': 9, 'salt': 8, 'specialty cheese': 8, 'canned vegetables': 8, 'sweet spreads': 8, 'mayonnaise': 8, 'dish cleaner': 7, 'cookware': 7, 'brandy': 7, 'chocolate marshmallow': 7, 'instant coffee': 7, 'flower soil/fertilizer': 7, 'flour': 6, 'Instant food products': 6, 'liquor (appetizer)': 5, 'cling film/bags': 5, 'house keeping products': 5, 'soups': 5, 'roll products': 5, 'cereals': 5, 'male cosmetics': 5, 'candles': 5, 'cooking chocolate': 5, 'popcorn': 4, 'cleaner': 4, 'pickled vegetables': 4, 'potato products': 4, 'light bulbs': 4, 'sauces': 4, 'liqueur': 4, 'female sanitary products': 3, 'soap': 3, 'frozen chicken': 3, 'artif. sweetener': 3, 'canned fish': 3, 'bathroom cleaner': 3, 'frozen fruits': 3, 'mustard': 3, 'tidbits': 3, 'decalcifier': 2, 'vinegar': 2, 'prosecco': 2, 'canned fruit': 2, 'rum': 2, 'bags': 2, 'whisky': 2, 'abrasive cleaner': 2, 'syrup': 2, 'cream': 2, 'softener': 2, 'jam': 2, 'hair spray': 1, 'baby cosmetics': 1, 'spices': 1, 'specialty fat': 1, 'snack products': 1, 'dental care': 1, 'kitchen towels': 1, 'meat spreads': 1, 'ketchup': 1, 'skin care': 1, 'rubbing alcohol': 1, 'nut snack': 1, 'cocoa drinks': 1, 'organic products': 1, 'cake bar': 1, 'honey': 1, 'kitchen utensil': 1, 'flower (seeds)': 1})


print(Counter(a[0:31]))
Counter({0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 27: 1, 28: 1, 29: 1, 30: 1, 31: 1})

使用道具

藤椅
1464316512 在职认证  发表于 2018-5-28 11:50:04 |只看作者 |坛友微信交流群
用value_counts

使用道具

板凳
sharlen_deng 发表于 2018-5-28 21:26:04 |只看作者 |坛友微信交流群
1464316512 发表于 2018-5-28 11:50
用value_counts
您好,我使用了a.apply(pd.value_counts) 命令 能得到各列元素出现的次数,请问有可以总结所有元素个数的命令吗?因为这个命令无法进行下一步画频数直方图

使用道具

报纸
hanhyojoo1992 发表于 2018-5-29 14:17:16 |只看作者 |坛友微信交流群
def change_dict(df)
    b={}
    for i in df.columns:
        b[i] = pd.to_dict(df[i]):
    return b
change_dict(a)
你试试这个

使用道具

地板
hanhyojoo1992 发表于 2018-5-29 14:19:09 |只看作者 |坛友微信交流群
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(df['Age'], bins=7)
plt.title('Age distribution')
plt.xlabel('Age')
plt.ylabel('Employee')
plt.show()
你仿照这个写一下

使用道具

7
sharlen_deng 发表于 2018-5-29 23:51:07 来自手机 |只看作者 |坛友微信交流群
hanhyojoo1992 发表于 2018-5-29 14:17
def change_dict(df)
    b={}
    for i in df.columns:
你好 感谢你的帮助
但是我在运行时报错 也不太懂这几行代码的意思
pd.to_dict是什么意思呢

使用道具

8
hanhyojoo1992 发表于 2018-5-31 09:54:25 |只看作者 |坛友微信交流群
sharlen_deng 发表于 2018-5-29 23:51
你好 感谢你的帮助
但是我在运行时报错 也不太懂这几行代码的意思
pd.to_dict是什么意思呢
转化字典的意思

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 08:45