擅长:python、mysql、java
<p>我不确定您的确切意思(示例表中的一些模拟数据会很好),但假设现在每一行都是一个单独的数据帧,每两列都是示例,是否需要这样做</p>
<pre><code># sample data
df = pd.DataFrame({
'sample1':[23.1, 12.2, 15.8],
'Unnamed:1':['alpha','beta','gamma'],
'sample2':[12.1, 13.4, 11.1],
'Unnamed:2':['alpha','beta','gamma'],
'sample3':[0.1,0.43,0.29],
'Unnamed:3':['alpha','beta','gamma']
})
</code></pre>
<div class="s-table-container">
^{tb1}$
</div>
<pre><code># initiate a blank dataframe
new_df = pd.DataFrame()
# filter columns by the sample number, then append to new_f
n = 3 # number of samples
for i in range(1,n+1):
temp_df = df[[col for col in df.columns if f'{i}' in col]]
temp_df.columns = 'wavelength','transmission'
temp_df['sample'] = i
new_df = new_df.append(temp_df)
new_df = new_df.reset_index(drop=True)
</code></pre>
<p>输出:</p>
<div class="s-table-container">
^{tb2}$
</div>
<p>所有数据关系仍然保留,您只需执行<code>new_df.groupby('wavelength').mean()</code>即可找到每个波长的平均值。将mean替换为<code>apply()</code>,并根据需要添加您自己的函数</p>