如何将Nan值更改为Dataframe中的特定数字?

时间:2021-05-14 04:06:59

标签: python pandas dataframe numpy

         DP 1    DP 2    DP 3     DP 4    DP 5    DP 6   DP 7    DP 8   DP 9    DP 10
OP 1     2.33    1.711   1.218   1.046   1.150   1.025   1.046   1.092  nan      -   
OP 2     3.043   1.691   1.362   1.174   1.067   1.048   1.051   1.059      
OP 3     4.054   1.717   1.238   1.132   1.068   1.056   1.045          
OP 4     3.014   1.748   1.327   1.103   1.093   1.116              
OP 5     2.798   1.862   1.241   1.242   1.148                  
OP 6     3.973   1.589   1.553   1.161                      
OP 7     3.372   1.552   1.458                          
OP 8     3.359   1.871                              
OP 9     3.494                                  
OP 10   

这是数据帧 DF1 ;

for ele in DF1:
    x = ele+2.0
print(x)

这将给出输出:

         DP 1     DP 2    DP 3    DP 4    DP 5    DP 6    DP 7    DP 8   DP 9   DP 10
OP 1     4.33    3.711   3.218   3.046   3.150   3.025   3.046   3.092    nan    -   
OP 2     5.043   3.691   3.362   3.174   3.067   3.048   3.051   3.059      
OP 3     6.054   3.717   3.238   3.132   3.068   3.056   3.045          
OP 4     5.014   3.748   3.327   3.103   3.093   3.116              
OP 5     4.798   3.862   3.241   3.242   3.148                  
OP 6     5.973   3.589   3.553   3.161                      
OP 7     5.372   3.552   3.458                          
OP 8     5.359   3.871                              
OP 9     5.494                                  
OP 10   

但我需要像这样的输出:

         DP 1     DP 2   DP 3     DP 4    DP 5    DP 6    DP 7    DP 8   DP 9   DP 10
OP 1     4.33    3.711   3.218   3.046   3.150   3.025   3.046   3.092   2.0     -   
OP 2     5.043   3.691   3.362   3.174   3.067   3.048   3.051   3.059      
OP 3     6.054   3.717   3.238   3.132   3.068   3.056   3.045          
OP 4     5.014   3.748   3.327   3.103   3.093   3.116              
OP 5     4.798   3.862   3.241   3.242   3.148                  
OP 6     5.973   3.589   3.553   3.161                      
OP 7     5.372   3.552   3.458                          
OP 8     5.359   3.871                              
OP 9     5.494                                  
OP 10   

这意味着如果我将 nan 添加到 number 那么它应该给出相应的数字。

4 个答案:

答案 0 :(得分:0)

这有帮助吗?

for ele in DF1:
    for ind,val in ele:
        if np.isnan(val):
            ele[ind] = 2.0
        else:
            ele[ind] = val+2.0

答案 1 :(得分:0)

如你所愿:

import pandas as pd
import numpy as np
    
data = [[1,10],[2,12],[3,13],[4,10],[5,12],[np.nan,13]]
df = pd.DataFrame(data,columns=['a','b'],dtype=float)
for element in df['a']:
    if(element >= 0):
        x = element + 2.0
    else:
        x = 2.0
    print(x)

简单的方法:


df.fillna(2.0)

答案 2 :(得分:0)

一种方法是简单地重新定义加法,使 x+nan 的计算结果为 x,但这相当危险。更安全的是定义一个自定义函数:

def nan_sum(a,b):
    if not a:
       return b
    if not b:
       return a
    return a+b

然后您可以将其应用于数据帧:DF1.applymap(lambda x: nan_sum(x,2.0))

答案 3 :(得分:0)

您可以使用 np.nan_to_num() 函数,该函数专门用于用零替换 nan。它的默认行为是用 nan 替换 0.0

import numpy as np

df.applymap(lambda x: np.nan_to_num(x)+2)