Counting the Frequency of words in a pandas data frame

后端 未结 3 1140
天命终不由人
天命终不由人 2020-12-04 18:01

I have a table like below:

      URN                   Firm_Name
0  104472               R.X. Yah & Co
1  104873        Big Building Society
2  109986            


        
3条回答
  •  时光说笑
    2020-12-04 18:47

    This answer can also be used - Count distinct words from a Pandas Data Frame. It utilizes the Counter method and applies it to each row.

    from collections import Counter
    c = Counter()
    df = pd.DataFrame(
        [[104472,"R.X. Yah & Co"],
        [104873,"Big Building Society"],
        [109986,"St James's Society"],
        [114058,"The Kensington Society Ltd"],
        [113438,"MMV Oil Associates Ltd"]
    ], columns=["URN","Firm_Name"])
    df.Firm_Name.str.split().apply(c.update)
    
    Counter({'R.X.': 1,
             'Yah': 1,
             '&': 1,
             'Co': 1,
             'Big': 1,
             'Building': 1,
             'Society': 3,
             'St': 1,
             "James's": 1,
             'The': 1,
             'Kensington': 1,
             'Ltd': 2,
             'MMV': 1,
             'Oil': 1,
             'Associates': 1})
    

提交回复
热议问题