擅长:python、mysql、java
<p>您可以通过以下方式获得想要的结果:</p>
<pre class="lang-py prettyprint-override"><code># get company value by date
avg = df.groupby(["Country_id", "Date", "Company_id"]).sum().unstack(level=2).loc[:, "Company_value"]
avg = pd.concat([avg, avg.shift(1)], axis=1)
avg["sum"] = avg.apply("sum", axis=1)
# get company count by date
counts = df.groupby(["Country_id", "Date"]).count().loc[:, "Company_value"]
counts2 = counts + counts.shift(1)
# get the "mean"
result = avg["sum"] / counts2.fillna(counts)
</code></pre>