擅长:python、mysql、java
<p>你可以试试这个:</p>
<pre><code>import io
temp = u""" user tag1 tag2 tag3
0 Roshan-ghai 0.0 1.0 1.0
1 mank-nion 1.0 1.0 2.0
2 pop-rajuel 2.0 0.0 1.0
3 random-guy 2.0 1.0 1.0"""
df = pd.read_csv(io.StringIO(temp), delim_whitespace=True)
maxtag1 = df.tag1.max()
maxtag2 = df.tag2.max()
maxtag3 = df.tag3.max()
number_users = len(df)
number_users_tag1 = len(df[df['tag1']!=0])
number_users_tag2 = len(df[df['tag2']!=0])
number_users_tag3 = len(df[df['tag3']!=0])
liste_values = [maxtag1,maxtag2,maxtag3,number_users,number_users_tag1,number_users_tag2,number_users_tag3]
</code></pre>
<p>然后创建一个函数,将行和这些值作为输入,并输出所需的三个值。并使用<code>apply</code>:</p>
<pre><code>output = df.apply(lambda x: yourfunction(x, list_values))
</code></pre>