擅长:python、mysql、java
<p>使用普通python总比依赖pandas好,因为如果列表很大,它会消耗大量内存。你知道吗</p>
<p>如果列表的大小为1000,那么在使用<code>expand = True</code>时,非1000长度的列表将具有Nan,这是对内存的浪费。试试这个。你知道吗</p>
<pre><code>df = pd.concat([df]*1000) # For the sake of large df.
%%timeit
df.actors_list.str.replace("(u\'|[\[\]]|\')",'').str.lower().str.split(',',expand=True).stack().value_counts()
10 loops, best of 3: 65.9 ms per loop
%%timeit
df['actors_list'] = df['actors_list'].str.strip('[]').str.replace(', ',',').str.split(',')
10 loops, best of 3: 24.1 ms per loop
%%timeit
words = {}
for i in df['actors_list']:
for w in i :
if w in words:
words[w]+=1
else:
words[w]=1
100 loops, best of 3: 5.44 ms per loop
</code></pre>