擅长:python、mysql、java
<p>我想有更好的方法,但这确实做到了:</p>
<pre><code>df = pd.DataFrame({
'building': ['A1', 'A1', 'A1', 'A1'],
'day': ['2019-07-02', '2019-07-02', '2019-07-03', '2019-07-03'],
'id': ['alak', 'ldau', 'lauid', 'lkdu'],
'counts': [1, 2, 3, 7],
'elevation': [5.7, 7.8, 8.7, 6.9],
'width':[1.2, 2.4, 3.4, 2.7]
})
df = df.set_index(['building','day'])
sum_count = df.groupby(['building','day']).counts.sum()
df['w_elevation'] = df.elevation*df.counts /sum_count
df['w_width'] = df.width*df.counts / sum_count
df.groupby(['building','day']).sum()
</code></pre>
<p>输出:</p>
<pre><code> counts elevation width w_elevation w_width
building day
A1 2019-07-02 3 13.5 3.6 7.10 2.00
2019-07-03 10 15.6 6.1 7.44 2.91
</code></pre>