擅长:python、mysql、java
<p>我使用了其他方法,而不是枢轴-</p>
<pre><code>areas = df_test["Area"].unique().tolist()
est_by_area = pd.DataFrame(index = areas)
est_by_area["Sum_of_Sub_Area"] = df_test["Area"].value_counts().to_frame()
df_test[["Milestone_1", "Milestone_2"]] = df_test[["Milestone_1", "Milestone_2"]].replace("-", np.nan)
est_by_area["Sum_of_Milestone_1"] = df_test.groupby("Area")["Milestone_1"].count().to_frame()
est_by_area["Sum_of_Milestone_2"] = df_test.groupby("Area")["Milestone_2"].count().to_frame()
</code></pre>
<p>我将回顾一下我在这里所做的:</p>
<p>首先,我创建了唯一的区域列表。
然后我创建了名为“est_by_area”的新数据框,而它的索引是我以前创建的区域列表。
之后,我将value_counts()方法指定给“Sum_of_Sub_Area”列,value_counts()方法提供唯一值和它们出现的次数。
在这之后,我对日期有了问题,我有两列“里程碑1”和“里程碑2”,其中混合了日期,字符串包含“-”表示空日期。
我在这里做的是将空日期转换为np.nan,因此当我对其使用方法时,它不会计算空日期(“-”)。
最后,我将df按“Area”列分组,计算每列具有值的时间,并将其分配给新的数据帧</p>
<p>结果:</p>
<pre><code> Sum_of_Sub_Area Sum_of_Milestone_2
Area_A 2 1
Area_B 1 0
</code></pre>
<p>希望有帮助</p>