如何获取数组中某些值的Nan值

时间:2017-04-11 13:37:22

标签: python pandas numpy

我正在生成一些随机数,我尝试应用条件,如果值> 80,然后把无,但我无法得到结果。我的代码如下

import pandas as pd
import numpy as np
from numpy import random

total = 200 
rand_numbers = np.random.randint(0, 100, total)
corrupt_values = np.random.randint(0, 100, total) > 80
flag = False
if flag:
    rand_numbers = [v for flag, v in zip(corrupt_values, rand_numbers)]
else:
   rand_numbers = None
print 'rand_numbers: ', rand_numbers

我想获得像

这样的结果
rand_numbers [20, 50, Nan, Nan, 40, 10] so that values greater than 80 are replaced by Nan

我尝试在rand_numbers中生成200个随机数,然后我创建一个条件,如果值超过80,那么它将放置NaN而不是值,否则为值。我试图压缩这两个数组并使这个条件工作,但我正在努力。我是编码的新手。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您似乎需要先将值转换为$user = new User(); $em = $this->getDoctrine()->getManager(); if ($request->getMethod() == 'POST') { $form->handleRequest($request); //Check for deleted duplication: $duplicatedUser = $em->getRepository('AppBundle:User') ->getDuplicatedAndDeletedUser($user); if($duplicatedUser != null){ $em->remove($duplicatedUser); $em->flush(); } if ($form->isValid()) { $em->persist($user); $em->flush(); } } (因为floatNaN),然后按条件更改值:

float

类似的解决方案(来自已删除的答案)与numpy.where

np.random.seed(100)
total = 100 
rand_numbers = np.random.randint(0, 100, total)
corrupt_values = rand_numbers > 80
print (rand_numbers)
[ 8 24 67 87 79 48 10 94 52 98 53 66 98 14 34 24 15 60 58 16  9 93 86  2 27
  4 31  1 13 83  4 91 59 67  7 49 47 65 61 14 55 71 80  2 94 19 98 63 53 27
 56 30 48 47 39 38 44 18 64 56 34 53 74 17 72 13 30 17 53 68 50 91 91 83 53
 78  0 13 57 76  3 70  3 84 79 10 87 60  3 48 52 43 36  5 71 38 86 94 98 42]

print (corrupt_values)
[False False False  True False False False  True False  True False False
  True False False False False False False False False  True  True False
 False False False False False  True False  True False False False False
 False False False False False False False False  True False  True False
 False False False False False False False False False False False False
 False False False False False False False False False False False  True
  True  True False False False False False False False False False  True
 False False  True False False False False False False False False False
  True  True  True False]

rand_numbers = rand_numbers.astype(float)
rand_numbers[corrupt_values] = None
print (rand_numbers)
[  8.  24.  67.  nan  79.  48.  10.  nan  52.  nan  53.  66.  nan  14.  34.
  24.  15.  60.  58.  16.   9.  nan  nan   2.  27.   4.  31.   1.  13.  nan
   4.  nan  59.  67.   7.  49.  47.  65.  61.  14.  55.  71.  80.   2.  nan
  19.  nan  63.  53.  27.  56.  30.  48.  47.  39.  38.  44.  18.  64.  56.
  34.  53.  74.  17.  72.  13.  30.  17.  53.  68.  50.  nan  nan  nan  53.
  78.   0.  13.  57.  76.   3.  70.   3.  nan  79.  10.  nan  60.   3.  48.
  52.  43.  36.   5.  71.  38.  nan  nan  nan  42.]

答案 1 :(得分:1)

你可以使用列表理解

import numpy as np

total = 200 
rand_numbers = np.random.randint(0, 100, total)

result=[i if i<=80 else float('NaN') for i in rand_numbers]

会给你:

 >>> result
[64, 23, 12, 8, 70, nan, 13, 19, 73, 18, 78, 25, 77, 45, nan, 6, 15, nan, nan, 47, nan, 39, 5, 9, 22, 59, 57, 71, 8, 24, 76, 33, 66, nan, 21, 39, 48, 23, 40, nan, nan, 75, 68, 17, 52, nan, 71, 55, 10, 53, 51, 21, 35, 6, 67, 10, 34, nan, 24, 11, 42, 72, 74, 40, 63, 8, 57, 10, nan, 45, nan, 18, nan, 80, 6, 21, 22, 2, 51, 54, 80, 50, 63, 40, nan, 26, 43, 65, 7, 13, 54, 69, 12, nan, nan, 40, 44, nan, 78, 45, 55, 72, 6, 46, 43, 33, 24, 69, 77, 51, 52, 51, nan, 32, 22, 54, 53, 25, 61, 32, 8, nan, 75, 9, 22, nan, nan, 54, 32, 49, nan, 8, 59, 44, 14, 62, 61, 37, 60, 56, 12, 23, 50, 76, 5, 14, 46, nan, 58, 18, 53, 18, 39, 10, 1, 17, 36, 31, 42, 71, 61, 39, 27, 79, nan, 44, 76, nan, 26, 3, 26, 19, 64, 6, 41, 65, 76, 31, nan, 12, nan, 77, 8, 49, nan, nan, nan, 5, 40, 15, nan, 42, 14, 12, 75, 54, 47, 65, 9, 12]

修改

也有可能:

import numpy as np

total = 200 
rand_numbers = np.random.randint(0, 100, total)
corrupt_values = rand_numbers > 80

result=[i[0] if i[1]==False else float('nan') for i in zip(rand_numbers, corrupt_values)]