最近在家找工作空閑時間比較多限嫌,剛好朋友最近一直在吐槽相親的事情靴庆,就爬了下豆瓣相親小組8月份發(fā)的貼子看看。
思路就是結巴分詞看看大家說的最多的是什么怒医,男女發(fā)帖情況撒穷,回應數(shù),年齡分布
數(shù)據(jù)來源
后裔采集器自動采集(我才不會說爬蟲學習曲線過分陡峭裆熙,太麻煩了。禽笑。入录。呢)
一共1165個記錄。
數(shù)據(jù)處理
#導包
import jieba
import pandas as pd
import re
import collections
#讀數(shù)據(jù)
df = pd.read_csv('C:/Users/Jasmine/Desktop/Learning/相親數(shù)據(jù)集.csv',engine='python')
# 刪除發(fā)帖內容為空的
df.drop(df[df['發(fā)帖內容'].isnull()].index,inplace=True)
# 把發(fā)帖內容的類型轉為字符串佳镜,方便后續(xù)分詞和政策匹配
df['發(fā)帖內容'] = df['發(fā)帖內容'].astype('str')
# 模式串僚稿,只保留中英文和數(shù)字
patten = r"[^\u4e00-\u9fa5^a-z^A-Z^0-9]"
#將匹配模式串進行編譯
re_obj = re.compile(patten)
def clear(text):
return re_obj.sub('', text)
df['發(fā)帖內容'] = df['發(fā)帖內容'].apply(clear)
# 用jieba分詞對每一個帖子進行分詞處理
def cut_words(words):
return jieba.lcut(words)
df['發(fā)帖內容'] = df['發(fā)帖內容'].apply(cut_words)
#讀取停用詞列表
stop_list = [
i.strip() for i in open(r'C:\Users\Jasmine\stopwords_zh.txt',encoding='gbk').readlines()
]
#移除停用詞函數(shù)
def remove_stop(words):
texts = []
# 遍歷詞列表里的每一個詞
for word in words:
if word not in stop_list: # 若不在停用詞列表中就將結果追加至texts列表中
texts.append(word)
return texts
df['發(fā)帖內容'] = df['發(fā)帖內容'].apply(remove_stop)
#將列表元素轉換為字符串,方便后續(xù)統(tǒng)計詞頻
def list_str(content):
return ' '.join(content)
df['content'] = df['發(fā)帖內容'].apply(list_str)
# 統(tǒng)計詞頻
text = ''
for s in df['content']:
text += s
data_cut = ' '.join(jieba.lcut(text))
frequency = collections.Counter(data_cut.split())
#取TOP200的詞
count_list = sorted(frequency.items(), key=lambda x:x[1],reverse=True)
count_list = count_list[:200]
#畫圖
wordcloud = charts.WordCloud()
wordcloud.add('',count_list,word_size_range=[20,100],shape=SymbolType.DIAMOND)
wordcloud.render_notebook()
#查找性別詞
def gender(word):
text = []
if "女朋友" in word or "征女" in word or "蒸女" in word:
text.append("男")
elif "男朋友" in word or "征男" in word or "蒸男" in word:
text.append("女")
return text
df['性別1'] = df['詳情標題'].apply(gender)
df['性別2'] = df['發(fā)帖內容'].apply(gender)
#正則匹配年齡字符串
pattern = r"[9]\d{1}|[8]\d{1}|[2]\d{1}|[3]\d|[1]\d{3}"
re_obj = re.compile(pattern)
def number(text):
return re_obj.findall(text)
df['年齡'] = df['發(fā)帖內容'].apply(number)
#將列表元素轉換為字符串(輸出到excel)
def list_str(content):
return ' '.join(content)
df['年齡'] = df['年齡'].apply(list_str)
def list_str(content):
return ' '.join(content)
數(shù)據(jù)展示
1.總計616個信息有效的帖子蟀伸,其中男性發(fā)帖371蚀同,女性發(fā)帖245,但男性發(fā)帖的回帖數(shù)量低于女性的回帖數(shù)量(女生還是比較內斂kkk)
對比
2.男性年齡分布:30-34歲最多啊掏,25-29次之蠢络,看來男生果然不著急,相親市場25+才是主角迟蜜,但35+的就少多了刹孔,但整體年齡焦慮還比較小(0-19是因為我把沒有年齡信息的帖子都標記為0娜睛,小聲diss相親不寫年齡這種蜜汁操作)
男生年齡分布
3.女生年齡分布:25-29區(qū)間最多髓霞,基本上是第二名30-34的兩倍了卦睹,感慨一下女生在婚戀問題上的焦慮感。方库。结序。。
女生年齡分布
4.發(fā)帖內容詞云圖
去除語氣詞纵潦,標點等徐鹤,截取詞頻top200的詞匯。kkk有點因錘絲聽:
“喜歡酪穿,希望":大家對戀愛凳干,婚姻還是比較樂觀的,還抱有美好的期待
“工作被济,身高救赐,性格”:硬指標
“160,170”:男生170只磷,女生160是個坎
Top200詞云