回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>假设我们有以下简化数据:</p>
<pre><code>df = pd.DataFrame({'A':list('abcd'),
'B':list('efgh'),
'Data_mean':[1,2,3,4],
'Data_std':[5,6,7,8],
'Data_corr':[9,10,11,12],
'Text_one':['foo', 'bar', 'foobar', 'barfoo'],
'Text_two':['bar', 'foo', 'barfoo', 'foobar'],
'Text_three':['bar', 'bar', 'barbar', 'foofoo']})
A B Data_mean Data_std Data_corr Text_one Text_two Text_three
0 a e 1 5 9 foo bar bar
1 b f 2 6 10 bar foo bar
2 c g 3 7 11 foobar barfoo barbar
3 d h 4 8 12 barfoo foobar foofoo
</code></pre>
<p>我要枚举具有相同前缀的列。在这种情况下,前缀是<code>Data, Text</code>。因此,<strong>预期产出</strong>将是:</p>
<pre><code> A B Data_mean1 Data_std2 Data_corr3 Text_one1 Text_two2 Text_three3
0 a e 1 5 9 foo bar bar
1 b f 2 6 10 bar foo bar
2 c g 3 7 11 foobar barfoo barbar
3 d h 4 8 12 barfoo foobar foofoo
</code></pre>
<p>请注意枚举列。你知道吗</p>
<hr/>
<p><strong>尝试的解决方案#1</strong>:</p>
<pre><code>def enumerate_cols(dataframe, prefix):
cols = []
num = 1
for col in dataframe.columns:
if col.startswith(prefix):
cols.append(col + str(num))
num += 1
else:
cols.append(col)
return cols
</code></pre>
<pre><code>enumerate_cols(df, 'Data')
['A',
'B',
'Data_mean1',
'Data_std2',
'Data_corr3',
'Text_one',
'Text_two',
'Text_three']
</code></pre>
<p><strong>尝试解决方案#2:</strong></p>
<pre><code>[c+str(x+1) for x, c in enumerate([col for col in df.columns if col.startswith('Data')])]
</code></pre>
<pre><code>['Data_mean1', 'Data_std2', 'Data_corr3']
</code></pre>
<p><strong>问题</strong>有没有更简单的解决方法,我也看了<code>df.filter(like='Data')</code>等,但这看起来也很牵强。你知道吗</p>
<hr/>
<p><strong>XY问题</strong><br/>
只是要确保我没有掉进<a href="https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem">XY problem</a>。我想使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.wide_to_long.html" rel="noreferrer">^{<cd3>}</a>,但是<code>stubnames</code>列需要有一个数字作为后缀,以便能够融化数据帧。你知道吗</p>
<p>引用文件:</p>
<blockquote>
<p>With stubnames [‘A’, ‘B’], this function expects to find one or more group of columns with format A-suffix1, A-suffix2,…, B-suffix1, B-suffix2,</p>
</blockquote>
<pre><code>pd.wide_to_long(df, stubnames=['Data', 'Text'], i=['A', 'B'], j='grp', sep='_')
</code></pre>
<p>这将返回一个空的数据帧。你知道吗</p>