擅长:python、mysql、java
<p>要使其工作,必须首先以不同的方式从文件中读取数据帧:在<code>pd.read_excel()</code>函数中设置参数<code>header=None</code>。因为现在你们的专栏被称为“REF”和“PLAYERS”,但我们想按他们分组</p>
<p>然后第一个列名可能是“0”,第一行如下所示,其中<code>df</code>是数据帧的名称:</p>
<pre><code># Set unique index for each group
df["group_id"] = (df[0] == "REF").cumsum()
</code></pre>
<p>解决方案:</p>
<pre><code># Set unique index for each group
df["group_id"] = (df["name_of_first_column"] == "REF").cumsum()
# Iterate over groups
dataframes = []
for name, group in df.groupby("group_id"):
df_ = group
# promote 1st row to column name
df_.columns = df_.iloc[0]
# and drop it
df_ = df_.iloc[1:]
# drop index column
df_ = df_[["REF", "PLAYERS"]]
# append to the list of dataframes
dataframes.append(df_)
</code></pre>
<p>所有多个数据帧现在都存储在一个数组<code>dataframes</code>中</p>