擅长:python、mysql、java
<p>创建一个新列,该列是离开和状态的组合</p>
<pre><code>df['comb'] = df.Departure + df.Status
df
# Key Departure Species1 Species2 Status comb
#0 1 R Carlan Carlan D RD
#1 1 R Scival Carex C RC
#2 2 R Carlan Scival D RD
#3 2 R Scival Bougra C RC
#4 3 D Carlan Carlan D DD
#5 3 D Scival Scival C DC
</code></pre>
<p>然后您可以按以下方式分组:</p>
^{pr2}$
<p>现在将结果组织到一个列表中,其中每个元素是一个元组<code>(column, Series(data, index))</code>,表示新数据帧中的单个数据点</p>
<pre><code>items = [ (key[1], pandas.Series( [len(val)], index=[key[0]] ) )for key,val in gb.groups.items() ]
</code></pre>
<p>并从这些项生成一个新的数据帧:</p>
<pre><code>result = pandas.from_items( items)
result
# RC DC DD RD
#Carlan NaN NaN 1 2
#Scival 2 1 NaN NaN
</code></pre>
<h3>额外信息</h3>
<p>请参见<a href="http://pandas.pydata.org/pandas-docs/stable/dsintro.html" rel="nofollow">this link</a>,了解如何从不同对象装箱新的数据帧。当您想从单个数据点(例如(Species1,comb))创建一个数据帧时,<code>from_items</code>是您的最佳选择。在</p>