![](/img/trans.png)
[英]How to count how many times a word is repeated in a column for a specific category in Python?
[英]Is there a way in which I can count how many times per day a specific word is present in specific column?
我正在分析Twitter數據集中感興趣的某些特定主題標簽的使用。 最終目標是隨着時間的推移可視化這些標簽的使用。 數據組織在pandas數據框中。 每行包含有關一條推文的信息。 其中一列稱為“文本”,這是所有推文所在的地方,一條推文是一個字符串。 該數據集在時間之后被索引,因此我要做的是計算每天使用特定主題標簽的次數。
因此,這是有關數據框的信息
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 9991 entries, 2018-05-25 15:54:01 to 2018-05-25 14:14:37
Data columns (total 13 columns):
Unnamed: 0 9991 non-null int64
ID 9991 non-null int64
has_media 2015 non-null object
is_reply 9991 non-null bool
is_retweet 9991 non-null bool
medias 2015 non-null object
nbr_favorite 9991 non-null int64
nbr_reply 9991 non-null int64
nbr_retweet 9991 non-null int64
text 9991 non-null object
url 9991 non-null object
user_id 9991 non-null int64
usernameTweet 9991 non-null object
dtypes: bool(2), int64(6), object(5)
memory usage: 956.2+ KB
特別是“文本”列
df['text']
給出以下結果:
datetime
2018-05-25 15:54:01 Høj stemmeprocent ved #ok18 urafstemning. Dej...
2018-05-25 16:40:24 Man kan tvivle på at de gode medarbejdere fra ...
2018-05-25 18:19:25 Nej @gitteredder teknikken drillede hos DLF. ...
2018-05-25 22:32:30 Rekordstor stemmeprocent hos @bibliotekarerne ...
2018-05-26 08:42:44 # ok18 stemte ja igår. Ja fordi folkeskolen i...
2018-05-26 10:21:20 Afstemningen er skudt i gang om #OK18 - 26 ti...
2018-05-26 12:12:28 Her godt et døgn efter afstemnings begyndelse ...
2018-05-26 14:14:35 Ikke vær bekymret for debatten - men vær bekym...
....
那么,如何計算每天使用#ok18標簽的次數,並在x軸上每天繪制一條線圖,在y軸上每天使用#標簽呢?
這將使您進入其中所有#ok18都出現的數據框:
df.loc[df['text'].str.lower().str.contains('#ok18') == True]
從那里開始,計數非常簡單,但是如果您要形象化,則可能不希望立即計數。 您將要針對時間軸繪制主題標簽的出現情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.