import numpy as np
import pandas as pd
import matplotlib
import matplotlib. pyplot as plt
import seaborn as sns
% matplotlib inline
df = pd. read_csv( './earphone_sentiment.csv' , encoding= 'utf-8' )
df. head( )
content_id
content
subject
sentiment_word
sentiment_value
0
0
Silent Angel期待您的光临,共赏美好的声音!
其他
好
1
1
2
这只HD650在1k的失真左声道是右声道的6倍左右,也超出官方规格参数范围(0.05%),看...
其他
NaN
0
2
3
达音科 17周年 倒是数据最好看,而且便宜
配置
好
1
3
4
bose,beats,apple的消費者根本不知道有曲線的存在
其他
NaN
0
4
5
不错的数据
配置
不错
1
df. info( )
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 17176 entries, 0 to 17175
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 content_id 17176 non-null int64
1 content 17176 non-null object
2 subject 17176 non-null object
3 sentiment_word 4966 non-null object
4 sentiment_value 17176 non-null int64
dtypes: int64(2), object(3)
memory usage: 671.1+ KB
df[ 'subject' ] . unique( )
array(['其他', '配置', '价格', '功能', '音质', '舒适', '外形'], dtype=object)
df[ 'sentiment_value' ] . unique( )
array([ 1, 0, -1])
df. groupby( 'sentiment_value' ) . size( ) . plot. bar( )
<AxesSubplot:xlabel='sentiment_value'>
import jieba
words= df. content. apply ( lambda t: list ( jieba. cut( t) ) )
words[ 0 ]
['Silent', ' ', 'Angel', '期待', '您', '的', '光临', ',', '共赏', '美好', '的', '声音', '!']
def clean_text ( value) :
import re
if value:
text= "" . join( re. findall( r"[\u4e00-\u9fff]+" , value) )
return text if len ( text) > 0 else None
else :
return None
df. content[ 2 ]
'达音科 17周年 倒是数据最好看,而且便宜'
print ( "Before Cleaning: %s" % df. content[ 2 ] )
print ( "After Cleaning: %s" % clean_text( df. content[ 2 ] ) )
print ( "Tokenize after cleaning: %s" % ", " . join( jieba. cut( clean_text( df. content[ 2 ] ) ) ) )
Before Cleaning: 达音科 17周年 倒是数据最好看,而且便宜
After Cleaning: 达音科周年倒是数据最好看而且便宜
Tokenize after cleaning: 达音科, 周年, 倒, 是, 数据, 最, 好看, 而且, 便宜
df[ 'text_clean' ] = df. content. apply ( lambda t: clean_text( t) )
df. text_clean. isnull( ) . sum ( )
333