<p>假设您在<code>df</code>中的列是<code>Name</code>以及任何带有后缀(例如,<code>_ID</code>,<code>_RE</code>)的后缀,那么您可以通过列名进行解析,首先提取所有<strong>唯一的</strong>可能的后缀:</p>
<pre><code># since the suffixes follow a pattern of `_*`, then I can look for the `_` character
suffixes = list(set([col[-3:] for col in df.columns if '_' in col]))
</code></pre>
<p>现在,有了后缀列表,接下来要创建一个现有数据帧的字典,其中字典中的键是后缀,值是带有后缀名的数据帧(例如<code>df_ID</code>,<code>df_RE</code>):</p>
^{pr2}$
<p>现在,您可以在<code>suffixes</code>列表中循环以提取列表中每个后缀的相应列,并执行合并和列提取:</p>
<pre><code>for suffix in suffixes:
cols = [col for col in df.columns if suffix in col]
dfs[suffix] = pd.merge(df, dfs[suffix], on='Name')
dfs[suffix] = dfs[suffix][cols]
</code></pre>
<p>现在有了后缀数据帧的字典。如果希望将数据帧作为单独的变量,而不是将它们保存在字典中,现在可以将它们设置为单个对象:</p>
<pre><code>df_ID = dfs['_ID']
df_RE = dfs['_RE']
... # and so forth
</code></pre>
<h3>在一个例子中把它们放在一起</h3>
<pre><code>import pandas as pd
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]})
# Get unique suffixes
suffixes = list(set([col[-3:] for col in df.columns if '_' in col]))
dfs = {} # dataframes dictionary
df_ID = pd.DataFrame({'Name': ['CTA15', 'CTA16', 'CFV', 'SAP', 'SOS']})
df_RE = pd.DataFrame({'Name': ['AC007']})
dfs['_ID'] = df_ID
dfs['_RE'] = df_RE
for suffix in suffixes:
cols = [col for col in df.columns if suffix in col]
dfs[suffix] = pd.merge(df, dfs[suffix], on='Name')
dfs[suffix] = dfs[suffix][cols]
df_ID = dfs['_ID']
df_RE = dfs['_RE']
print(df_ID)
print(df_RE)
</code></pre>
<p><strong>结果:</strong></p>
<pre><code> AA_ID BB_ID CC_ID
0 22 4 2
1 22 5 2
DD_RE EE_RE
0 8 9
1 9 9
2 0 10
</code></pre>