擅长:python、mysql、java
<p>我认为不可能使用read_csv,您可以在加载后修改索引,即:</p>
<pre><code>from io import StringIO
txt = '''Name,Height,Age
"",Metres,""
A,-1,25
B,95,-1'''
df = pd.read_csv(StringIO(txt),header=[0,1],na_values=['-1',''])
df.columns = df.columns.set_levels(df.columns.get_level_values(level=1).str.replace('Un.*',''),level=1)
df.columns = df.columns.set_levels(df.columns.get_level_values(level=1).str.replace('Un.*',''),level=1)
</code></pre>
<p>输出:</p>
^{pr2}$
<p>懂得分配数据框列您可以检查两次<a href="https://stackoverflow.com/questions/48061197/pandas-set-levels-how-to-avoid-sorting-of-data">here</a>。它仍然很神秘</p>
<p>编辑,<code>set_levels</code>仍然有缺陷,您可以使用:</p>
<pre><code>df.columns = df.columns.set_levels(df.columns.levels[1].str.replace('Un.*', ''), level=1)
</code></pre>