回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有3个数据帧,
测向</p>
<pre><code>df = pd.DataFrame({'Name': ['CTA15', 'CTA16', 'AC007', 'AC007', 'AC007'],
'AA_ID': [22, 22, 2, 2, 2],
'BB_ID':[4, 5, 6, 8, 9],
'CC_ID' : [2, 2, 3, 3, 3],
'DD_RE': [4,7,8,9,0],
'EE_RE':[5,8,9,9,10]})
</code></pre>
<p>和df逖ID</p>
^{pr2}$
<p>另一个是<code>df_RE</code>,这两个数据帧都有列名,所以我需要将它合并到数据帧df中,然后根据数据帧名称的最后一部分来选择列。也就是说,例如,如果数据帧是<code>df_ID</code>,那么我需要所有以<code>"ID" + "Name"</code>结尾的列,用于来自数据帧<code>df</code>的所有匹配行,如果数据帧id <code>df_REL</code>,那么我需要df中所有以<code>"RE" + "Name"</code>结尾的列,并且我想单独保存它。
我知道我可以在循环内部打电话</p>
<pre><code>for dfs in dataframes:
ID=[col for col in df.columns if '_ID' in col]
df_ID=pd.merge(df,df_ID,on='Name')
df_ID=df_ID[ID]
</code></pre>
<p>但是这里的ID,必须在数据帧以RE结尾时再次更改,等等,我有两个不同字符串的文件,所以任何更好的解决方案都会很好</p>
<p>最后,我需要df_ID,因为所有列都以ID结尾</p>
<pre><code> df_ID = pd.DataFrame({'Name': ['CTA15', 'CTA16'],
'AA_ID': [22, 22'],
'BB_ID':[4, 5],
'CC_ID' : [2, 2]})
</code></pre>
<p>任何帮助都很好</p>