熊猫将功能应用于多列并创建多列以存储结果

时间:2019-09-02 14:02:42

标签: python python-3.x pandas entropy

我有以下df

country    street           postcode    id
  SA                         XX0         1
  GB       17 abc road                   2
  BE       129 def street    127         3
  US       nan               nan         4

我想为countrystreetpostcode的值计算熵;空字符串或NaN将默认获得0.25的值;

from entropy import shannon_entropy

vendor_fields_to_measure_entropy_on = ('country', 'vendor_name', 'town', 'postcode', 'street')

fields_to_update = tuple([key + '_entropy_val' for key in vendor_fields_to_measure_entropy_on])

for fields in zip(vendor_fields_to_measure_entropy_on, fields_to_update):
    entropy_score = []

    for item in df[fields[0]].values:
        item_as_str = str(item)
        if len(item_as_str) > 0 and item_as_str != 'NaN':
           entropy_score.append(shannon_entropy(item_as_str))
        else:
           entropy_score.append(.25)

    df[fields[1]] = entropy_score

我想知道什么是最好的方法,所以结果看起来像

 country    street           postcode    id    
  SA                         XX0         1                        
  GB       17 abc road                   2
  BE       129 def street    127         3
  US       nan               nan         4   


 country_entropy_val  street_entropy_val  postcode_entropy_val
  0.125               0.25                0.11478697512328288
  0.125               0.38697440929431765 0.25
  0.125               0.39775073104910885 0.19812031562256
  0.125               0.25                0.25

2 个答案:

答案 0 :(得分:1)

v-checkbox

答案 1 :(得分:1)

predicate = predicate.and(…)

结果:

predicate