python绘制词云图

python绘制词云图

简介:本文讲解如何通过python绘制词云图。

需要注意的是,需要将代码中的your_excel_file_path.xlsx替换为你自己的Excel文件路径,column_name替换为你要生成词云图的那一列的列名。另外,还可以根据需要调整参数,如停用词、词云图大小、背景颜色等。

效果展示
python绘制词云图_第1张图片

import pandas as pd
import jieba
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取评论数据
df = pd.read_excel(r'D:\系统默认\桌面\京东评论-当前商品最近评论.xlsx')
# 文本预处理
df['comment'] = df['comment'].apply(lambda x: ' '.join(jieba.lcut(x))) # 分词
# 读取停用词
with open('D:\系统默认\桌面\stop_words_eng.txt', 'r', encoding='utf-8') as f:
stop_words = f.read().split('\n')
df['comment'] = df['comment'].apply(lambda x: ' '.join([word for word in
x.split() if word not in stop_words])) # 去除停用词
# 统计单词出现频率
words = []
for comment in df['comment']:
words += comment.split()
word_count = Counter(words).most_common(100)
print(word_count)
生成词云图
font_path = r'C:\Windows\Fonts\msyh.ttc' # 指定微软雅黑字体路径
wordcloud = WordCloud(width=800, height=600, background_color='white',
font_path=font_path).generate_from_frequencies(word_count)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

这里给一份常用的停词
stop_words_eng.txt

a
aaaaah	
aaahhh	
aaaoe	
aah	
about
above
ac
according
accordingly
across
actually
ad
adj
af
after
afterwards
again
against
al
albeit
all
almost
alone
along
already
als
also
although
always
am
among
amongst
amoungst
amount
an
and
another
any
anybody
anyhow
anyone
anything
anyway
anywhere
ap
apart
apparently
are
aren
arise
around
as
aside
at
au
auf
aus
aux
av
avec
away
b
back
be
became
because
become
becomes
becoming
been
before
beforehand
began
begin
beginning
begins
behind
bei
being
below
beside
besides
best
better
between
beyond
bill
billion
both
bottom
briefly
but
by
c
call
came
can
cannot
canst
cant
caption
captions
certain
certainly
cf
choose
chooses
choosing
chose
chosen
clear
clearly
co
come
comes
computer
con
contrariwise
cos
could
couldn
couldnt
cry
cu
d
da
dans
das
day
de
degli
dei
del
della
delle
dem
den
der
deren
des
describe
detail
di
did
didn
die
different
din
do
does
doesn
doing
don
done
dos
dost
double
down
du
dual
due
durch
during
e
each
ed
eg
eight
eighty
either
el
eleven
else
elsewhere
em
empty
en
end
ended
ending
ends
enough
es
especially
et
etc
even
ever
every
everybody
everyone
everything
everywhere
except
excepted
excepting
exception
excepts
exclude
excluded
excludes
excluding
exclusive
f
fact
facts
far
farther
farthest
few
ff
fifteen
fifty
fify
fill
finally
find
fire
first
five
foer
follow
followed
following
follows
for
former
formerly
forth
forty
forward
found
four
fra
frequently
from
front
fuer
full
further
furthermore
furthest
g
gave
general
generally
get
gets
getting
give
given
gives
giving
go
going
gone
good
got
great
greater
h
had
haedly
half
halves
hardly
has
hasn
hasnt
hast
hath
have
haven
having
he
hence
henceforth
her
here
hereabouts
hereafter
hereby
herein
hereto
hereupon
hers
herself
het
high
higher
highest
him
himself
hindmost
his
hither
how
however
howsoever
hundred
hundreds
i
ie
if
ihre
ii
im
immediately
important
in
inasmuch
inc
include
included
includes
including
indeed
indoors
inside
insomuch
instead
interest
into
inward
is
isn
it
its
itself
j
ja
journal
journals
just
k
kai
keep
keeping
kept
kg
kind
kinds
km
l
la
large
largely
larger
largest
las
last
later
latter
latterly
le
least
les
less
lest
let
like
likely
little
ll
long
longer
los
low
lower
lowest
ltd
m
made
mainly
make
makes
making
many
may
maybe
me
meantime
meanwhile
med
might
mill
million
mine
miss
mit
more
moreover
most
mostly
move
mr
mrs
ms
much
mug
must
my
myself
n
na
nach
name
namely
nas
near
nearly
necessarily
necessary
need
needed
needing
needs
neither
nel
nella
never
nevertheless
new
next
nine
ninety
no
nobody
none
nonetheless
noone
nope
nor
nos
not
note
noted
notes
nothing
noting
notwithstanding
now
nowadays
nowhere
o
obtain
obtained
obtaining
obtains
och
of
off
often
og
ohne
ok
old
om
on
once
onceone
one
only
onto
or
ot
other
others
otherwise
ou
ought
our
ours
ourselves
out
outside
over
overall
owing
own
p
par
para
part
particular
particularly
past
per
perhaps
please
plenty
plus
por
possible
possibly
pour
poured
pouring
pours
predominantly
previously
pro
probably
prompt
promptly
provide
provided
provides
providing
put
q
quite
r
rather
re
ready
really
recent
recently
regardless
relatively
respectively
reuters
round
s
said
same
sang
save
saw
say
second
see
seeing
seem
seemed
seeming
seems
seen
sees
seldom
self
selves
send
sending
sends
sent
serious
ses
seven
seventy
several
shall
shalt
she
short
should
shouldn
show
showed
showing
shown
shows
si
side
sideways
significant
similar
similarly
simple
simply
since
sincere
sing
single
six
sixty
sleep
sleeping
sleeps
slept
slew
slightly
small
smote
so
sobre
some
somebody
somehow
someone
something
sometime
sometimes
somewhat
somewhere
soon
spake
spat
speek
speeks
spit
spits
spitting
spoke
spoken
sprang
sprung
staves
still
stop
strongly
substantially
successfully
such
sui
sulla
sung
supposing
sur
system
t
take
taken
takes
taking
te
ten
tes
than
that
the
thee
their
theirs
them
themselves
then
thence
thenceforth
there
thereabout
thereabouts
thereafter
thereby
therefor
therefore
therein
thereof
thereon
thereto
thereupon
these
they
thick
thin
thing
things
third
thirty
this
those
thou
though
thousand
thousands
three
thrice
through
throughout
thru
thus
thy
thyself
til
till
time
times
tis
to
together
too
top
tot
tou
toward
towards
trillion
trillions
twelve
twenty
two
u
ueber
ugh
uit
un
unable
und
under
underneath
unless
unlike
unlikely
until
up
upon
upward
us
use
used
useful
usefully
user
users
uses
using
usually
v
van
various
ve
very
via
vom
von
voor
vs
w
want
was
wasn
way
ways
we
week
weeks
well
went
were
weren
what
whatever
whatsoever
when
whence
whenever
whensoever
where
whereabouts
whereafter
whereas
whereat
whereby
wherefore
wherefrom
wherein
whereinto
whereof
whereon
wheresoever
whereto
whereunto
whereupon
wherever
wherewith
whether
whew
which
whichever
whichsoever
while
whilst
whither
who
whoever
whole
whom
whomever
whomsoever
whose
whosoever
why
wide
widely
will
wilt
with
within
without
won
worse
worst
would
wouldn
wow
x
xauthor
xcal
xnote
xother
xsubj
y
ye
year
yes
yet
yipee
you
your
yours
yourself
yourselves
yu
z
za
ze
zu
zum

你可能感兴趣的:(python数据分析,python,开发语言)