将一组函数应用于数据框

时间:2017-09-07 00:51:45

标签: python pandas dataframe apply

我试图找出基于某种用户" config"功能化数据帧转换的最佳/最有效方法。

配置背后的想法是拥有一组默认的转换,然后允许用户自定义其他转换并将它们添加到字典中。然后他们都会在"运行时"。

中得到应用

我想要某种数据框

<script>
$('#audio-rep-id').click(function() {

  var aud = document.getElementById("audio-file-id"); 
  aud.play();
  curTime = parseInt(aud.currentTime); 
  durTime = parseInt(aud.duration);

  setInterval(function (){
    if(durTime > 59){
      var bit_1 = durTime / 60;
      bit_1 = Math.floor(bit_1);
      var bit_2 =   durTime % 60;
      bit_2  = bit_2 * 60; 
      if(bit_1 < 10){
        $('#total_Time').text('0' + bit_1 + ':' + bit_2/60);
      }else{ $('#total_Time').text(bit_1 + ':' + bit_2/60);}

    }else{$('#total_Time').text('00:' + durTime);}

    if(curTime > 59){
      var bit_1 = curTime / 60;
      bit_1 = Math.floor(bit_1);
      var bit_2 =   curTime % 60;
      bit_2  = bit_2 * 60; 
      if(bit_1 < 10){
        $('#current_Time').text('0' + bit_1 + ':' + bit_2/60);
      }else{ $('#current_Time').text(bit_1 + ':' + bit_2/60);}

    }else{$('#current_Time').text('00:' + curTime);}

  },1000);
});

var aud = document.getElementById("audio-file-id"); 
$('#').click(function() {
  aud.play();
});

</script>

和某种要应用的函数字典。理想情况下,接受一元和非一元函数。

可以执行类似

之类的设置
<div class="hidden-audio-wrapper1">
  <audio controls id="audio-file-id">
    <source src="horse.ogg" type="audio/ogg">
    <source src="horse.mp3" type="audio/mpeg">
    Your browser does not support the audio element.
  </audio>
</div>


<div class="d-flex flex-row text-primary primary" style="background-color:#f9f9f9; width:80%; height:40px; border-radius:35px; border:1px solid;">
  <div class="p-2">
    <input id="progress" type="range" value="40" />
  </div> 
  <div class="p-2">
    <span class="fa fa-play-circle fa-lg" style="font-size:28px; "></span>
    <span class="fa fa-pause-circle fa-lg" style="font-size:28px; display:none"></span>
  </div>
  <div id = "time" class="p-2"> 
    <span id="total_Time">00:00</span> -  
  </div>
  <div id = "time" class="p-2"><span id="current_Time">00:00</span>
  </div>
  <div class="p-2">
    <input id="volume" style="width:50px; zoom:100%;" type="range" value="40" />
  </div>
</div>

    'a' 'b'
0    1   2
1    1   0
2    3   1

现在,我有类似

的东西
df['bool'] = df['a'] > 1

然而,我意识到这非常非常低效,特别是随着功能数量的增加。因为每次都在每行上应用循环,除了失去广播的好处/你在上面的布尔语句之类的东西中所谓的内容。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

为什么不放弃apply并将整个数据框传递给lambda

for k, f in config.iteritems():
    df[k] = f(df)

print(df)

   a  b  sumval   bool
0  1  2       3  False
1  1  0       1  False
2  3  1       4   True

pandas&#39;广播的操作是神奇的。

答案 1 :(得分:2)

您可以将config中的公式作为列表进行跟踪。然后,您可以将它们传递给由eval分隔的'\n'方法。

config = ['bool = a > 1', 'sumVal = a + b']

df.eval('\n'.join(config), inplace=False)

   a  b   bool  sumVal
0  1  2  False       3
1  1  0  False       1
2  3  1   True       4

你可以看到

'\n'.join(config)

评估到

'bool = a > 1\nsumVal = a + b'

或者类似于字典

config = {'bool': 'a > 1', 'sumVal': 'a + b'}

j, f = '\n'.join, '{} = {}'.format

df.eval(j([f(k, v) for k, v in config.items()]), inplace=False)

   a  b   bool  sumVal
0  1  2  False       3
1  1  0  False       1
2  3  1   True       4

在这种情况下

j([f(k, v) for k, v in config.items()])

还评估为

'bool = a > 1\nsumVal = a + b'