熊猫-填写缺失值,从正态分布中选择值

时间:2018-11-01 15:43:51

标签: python pandas dataframe missing-data

下面的代码将仅生成正态分布的一个值,并使用相同的值填充所有缺失的值:

<?php 

/**
* @file
* Contains onesignal_api.module.
* 
*/

use Drupal\Core\Entity\EntityInterface;

/***
* Hook into OneSignal API to send push notifications once a new node is created
*/

function onesignal_api_insert(\Drupal\Core\Entity\EntityInterface $node) {
if($node->isNew()) {
    function sendMessage() {
      $content      = array(
          "en" => 'New Node Created'
      );
      $hashes_array = array();
      array_push($hashes_array, array(
          "id" => "like-button",
          "text" => "Like",
          "icon" => "http://i.imgur.com/N8SN8ZS.png",
          "url" => "http://push-test/"
      ));
      array_push($hashes_array, array(
          "id" => "like-button-2",
          "text" => "Like2",
          "icon" => "http://i.imgur.com/N8SN8ZS.png",
          "url" => "http://push-test/"
      ));
      $fields = array(
          'app_id' => "XXXXXXXXX",
          'include_player_ids' => array("XXXXXX","XXXXX","XXXXXX"),
          'data' => array(
              "foo" => "bar"
          ),
          'contents' => $content,
          'web_buttons' => $hashes_array
      );

      $fields = json_encode($fields);
      print("\nJSON sent:\n");
      print($fields);

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Content-Type: application/json; charset=utf-8',
          'Authorization: Basic XXXXXXX'
      ));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($ch, CURLOPT_HEADER, FALSE);
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

      $response = curl_exec($ch);
      curl_close($ch);

      return $response;
  }

  $response = sendMessage();
  $return["allresponses"] = $response;
  $return = json_encode($return);

  $data = json_decode($response, true);
  print_r($data);
  $id = $data['id'];
  print_r($id);

  print("\n\nJSON received:\n");
  print($return);
  print("\n");

 }//if Node is new
}//func hook signal

我们该怎么做才能为每个缺失值生成一个值?

2 个答案:

答案 0 :(得分:2)

您可以创建具有正常值的系列。您应该提取您正在处理的列中的 Nan 值的索引。

df:你的数据框

col:包含 Nan 值的 col

final_lines["list-price"] = final_lines.apply(lambda x: x["list-price"]*-1 if x["ordtype"] != "I" else x["list-price"])

答案 1 :(得分:0)

您可以创建一系列长度与数据帧相同的随机变量,然后应用fillna:

df.fillna(pd.Series([np.random.normal() for x in range(len(df))]))

如果连续缺少一个值,fillna会忽略它。