擅长:python、mysql、java
<p>有一种方法:</p>
<pre><code>import pandas as pd
import numpy as np
from io import StringIO
df = pd.read_fwf(StringIO("""
Post userid Weight Response
text1 A 1 0
text2 A 1 1
text1 B 1 0
text2 C 1 1
text1 C 1 1
text2 B 1 0
"""), header=1)
mn = df.groupby(['Post'])['Response'].mean()
md = df.groupby(['Post'])['Response'].agg(lambda x: max(np.mean(x), 1 - np.mean(x)))
mn.name = 'Avg_Response'
md.name = 'Agreement'
df2 = pd.concat([mn, md], axis=1)
df2
</code></pre>
<p>屈服。。。你知道吗</p>
<pre><code> Avg_Response Agreement
Post
text1 0.333333 0.666667
text2 0.666667 0.666667
</code></pre>