How to make a histogram from a list of strings in Python?

前端 未结 8 1230
说谎
说谎 2020-12-03 04:29

I have a list of strings:

a = [\'a\', \'a\', \'a\', \'a\', \'b\', \'b\', \'c\', \'c\', \'c\', \'d\', \'e\', \'e\', \'e\', \'e\', \'e\']

I w

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 05:10

    Very easy with Pandas.

    import pandas
    from collections import Counter
    a = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e']
    letter_counts = Counter(a)
    df = pandas.DataFrame.from_dict(letter_counts, orient='index')
    df.plot(kind='bar')
    

    Notice that Counter is making a frequency count, so our plot type is 'bar' not 'hist'.

    histogram of letter counts

提交回复
热议问题