对python中的每个dataframe元素应用相同的计算

时间:2016-06-06 07:21:58

标签: python pandas dataframe data-analysis large-data

我有一个像这样的数据框。

private void button7_Click(object sender, EventArgs e)
{
    List<OpenPop.Mime.Message> allaEmail = FetchAllMessages(...);

    StringBuilder builder = new StringBuilder();
    foreach(OpenPop.Mime.Message message in allaEmail)
    {
         OpenPop.Mime.MessagePart plainText = message.FindFirstPlainTextVersion();
         if(plainText != null)
         {
             // We found some plaintext!
             builder.Append(plainText.GetBodyAsText());
         } else
         {
             // Might include a part holding html instead
             OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion();
             if(html != null)
             {
                 // We found some html!
                 builder.Append(html.GetBodyAsText());
             }
         }
    }
    MessageBox.Show(builder.ToString());
}

我必须对每一行应用计算。这是每个元素x

          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

我用##来描述那个特定的值。我必须为数据帧的每个元素执行此操作,这是最有效的方法,因为我有一个很大的数字。元素。我正在使用python2.7。 输出:

x =(( specific tag's count for that user ##that element itself##))/ max no. of count of that tag ##max value of that column##)) * (ln(no. of total user ##lenth of df##)/(no. of of user having that tag ##no. of user having non 0 count for that particular tag or column ##))

我刚刚使用了我为mank nion和tag1编写的公式 x =((1.0)/2.0)*(ln(4/3)= .143。

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

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]

然后创建一个函数,该函数将行和这些值作为输入,并输出所需的三个值。并使用apply

output = df.apply(lambda x: yourfunction(x, list_values))

答案 1 :(得分:1)

您可以先ix选择没有第一列的所有值。然后使用非{0}的maxsumnumpy.log

import pandas as pd
import numpy as np

print (df.ix[:, 'tag1':].max())
tag1    2.0
tag2    1.0
tag3    2.0
dtype: float64

print ((df.ix[:, 'tag1':] != 0).sum())
tag1    3
tag2    3
tag3    4
dtype: int64

df.ix[:, 'tag1':] = (df.ix[:, 'tag1':] / df.ix[:, 'tag1':].max() * 
                    (np.log(len(df) / (df.ix[:, 'tag1':] != 0).sum())))

print (df)
          user      tag1      tag2  tag3
0  Roshan-ghai  0.000000  0.287682   0.0
1    mank-nion  0.143841  0.287682   0.0
2   pop-rajuel  0.287682  0.000000   0.0
3   random-guy  0.287682  0.287682   0.0

iloc的另一个解决方案:

df1 = df.iloc[:, 1:]
df.iloc[:, 1:] = (df1 / df1.max() * (np.log(len(df) / (df1 != 0).sum())))
print (df)
          user      tag1      tag2  tag3
0  Roshan-ghai  0.000000  0.287682   0.0
1    mank-nion  0.143841  0.287682   0.0
2   pop-rajuel  0.287682  0.000000   0.0
3   random-guy  0.287682  0.287682   0.0