<p>编辑:</p>
<p>我认为您需要先通过<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html" rel="nofollow noreferrer">^{<cd2>}</a>+<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.stack.html" rel="nofollow noreferrer">^{<cd3>}</a>来重塑<code>separate_data</code>,通过<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename_axis.html" rel="nofollow noreferrer">^{<cd4>}</a>设置索引名,通过重命名设置<code>Serie</code>的名称。在</p>
<p>然后,两个级别都可以使用一些函数<code>groupby</code>。在</p>
<p>然后<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.join.html" rel="nofollow noreferrer">^{<cd7>}</a>到{<cd8>},使用默认的左连接:</p>
<pre><code>separate_data1 =separate_data.set_index('x').stack().rename_axis(('x','field_1')).rename('d')
print (separate_data1)
x field_1
1 a 1
b 3
c 7
2 a 2
b 5
c 6
3 a 2
b 4
c 4
4 a 2
b 5
c 9
5 a 6
b 1
c 10
Name: d, dtype: int64
</code></pre>
<p>如有必要,请使用某些函数,主要是如果<code>x</code>与<code>field_1</code>成对出现重复,则返回漂亮的唯一对:</p>
^{pr2}$
<hr/>
<p>我认为您不能使用<code>transform</code>,因为多个列是一起读取的。在</p>
<p>所以使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.apply.html" rel="nofollow noreferrer">^{<cd12>}</a>:</p>
<pre><code>df1 = benchmark.groupby(['field_1']).apply(func)
</code></pre>
<p>然后对于新列有多个解决方案,例如使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.join.html" rel="nofollow noreferrer">^{<cd7>}</a>(默认<code>left join</code>)或{a7}。在</p>
<p>两种方法的样品溶液为<a href="https://stackoverflow.com/a/43979558/2901002">here</a>。在</p>
<p>{16{a16>可以使用^ new}或列。在</p>