<p>除了其他答案,在a <code>Series</code>中还有<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html" rel="noreferrer">map</a>和<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.apply.html" rel="noreferrer">apply</a>。</p>
<p><strong>Apply可以从序列中生成数据帧;但是,map只会在另一个序列的每个单元格中放置一个序列,这可能不是您想要的。</p>
<pre><code>In [40]: p=pd.Series([1,2,3])
In [41]: p
Out[31]:
0 1
1 2
2 3
dtype: int64
In [42]: p.apply(lambda x: pd.Series([x, x]))
Out[42]:
0 1
0 1 1
1 2 2
2 3 3
In [43]: p.map(lambda x: pd.Series([x, x]))
Out[43]:
0 0 1
1 1
dtype: int64
1 0 2
1 2
dtype: int64
2 0 3
1 3
dtype: int64
dtype: object
</code></pre>
<p>另外,如果我有一个带有副作用的函数,比如“连接到web服务器”,我可能会使用<code>apply</code>,只是为了清楚起见。</p>
<pre><code>series.apply(download_file_for_every_element)
</code></pre>
<p><strong><code>Map</code>不仅可以使用函数,还可以使用字典或其他系列。</strong>假设您要操作<a href="http://en.wikipedia.org/wiki/Permutation" rel="noreferrer">permutations</a>。</p>
<p>采取</p>
<pre><code>1 2 3 4 5
2 1 4 5 3
</code></pre>
<p>这个排列的平方是</p>
<pre><code>1 2 3 4 5
1 2 5 3 4
</code></pre>
<p>你可以用<code>map</code>来计算。不确定是否记录了self-application,但它可以在<code>0.15.1</code>中工作。</p>
<pre><code>In [39]: p=pd.Series([1,0,3,4,2])
In [40]: p.map(p)
Out[40]:
0 0
1 1
2 4
3 2
4 3
dtype: int64
</code></pre>