在pandas中使用固定列对多个列应用操作

时间:2016-08-30 06:44:16

标签: python pandas dataframe sum multiple-columns

我有一个如下所示的数据框。最后一列显示了所有列的值之和,即ABDKT。请注意,有些列也有NaN

word1,A,B,D,K,T,sum
na,,63.0,,,870.0,933.0
sva,,1.0,,3.0,695.0,699.0
a,,102.0,,1.0,493.0,596.0
sa,2.0,487.0,,2.0,15.0,506.0
su,1.0,44.0,,136.0,214.0,395.0
waw,1.0,9.0,,34.0,296.0,340.0

如何计算每行的熵?即我应该找到类似下面的内容

df['A']/df['sum']*log(df['A']/df['sum']) + df['B']/df['sum']*log(df['B']/df['sum']) + ...... + df['T']/df['sum']*log(df['T']/df['sum'])

条件是,只要log中的值变为zeroNaN,整个值就应该被视为零(根据定义,日志将返回错误作为日志0未定义)。

我知道使用lambda操作来应用于各个列。在这里,我无法想到一个纯粹的熊猫解决方案,其中固定列sum应用于不同的列ABD等。虽然我能想到对带有硬编码列值的CSV文件进行简单的循环迭代。

2 个答案:

答案 0 :(得分:5)

我认为您可以使用ixAT选择列,然后用div除以numpy.log。上次使用sum

print (df['A']/df['sum']*np.log(df['A']/df['sum']))
0         NaN
1         NaN
2         NaN
3   -0.021871
4   -0.015136
5   -0.017144
dtype: float64

print (df.ix[:,'A':'T'].div(df['sum'],axis=0)*np.log(df.ix[:,'A':'T'].div(df['sum'],axis=0)))
          A         B   D         K         T
0       NaN -0.181996 NaN       NaN -0.065191
1       NaN -0.009370 NaN -0.023395 -0.005706
2       NaN -0.302110 NaN -0.010722 -0.156942
3 -0.021871 -0.036835 NaN -0.021871 -0.104303
4 -0.015136 -0.244472 NaN -0.367107 -0.332057
5 -0.017144 -0.096134 NaN -0.230259 -0.120651

print((df.ix[:,'A':'T'].div(df['sum'],axis=0)*np.log(df.ix[:,'A':'T'].div(df['sum'],axis=0)))
         .sum(axis=1))
0   -0.247187
1   -0.038471
2   -0.469774
3   -0.184881
4   -0.958774
5   -0.464188
dtype: float64

答案 1 :(得分:1)

df1 = df.iloc[:, :-1]
df2 = df1.div(df1.sum(1), axis=0)
df2.mul(np.log(df2)).sum(1)

word1
na    -0.247187
sva   -0.038471
a     -0.469774
sa    -0.184881
su    -0.958774
waw   -0.464188
dtype: float64

设置

from StringIO import StringIO
import pandas as pd

text = """word1,A,B,D,K,T,sum
na,,63.0,,,870.0,933.0
sva,,1.0,,3.0,695.0,699.0
a,,102.0,,1.0,493.0,596.0
sa,2.0,487.0,,2.0,15.0,506.0
su,1.0,44.0,,136.0,214.0,395.0
waw,1.0,9.0,,34.0,296.0,340.0"""

df = pd.read_csv(StringIO(text), index_col=0)

df

enter image description here