根据pandas中的字符串用NaN替换列

时间:2017-01-25 12:17:51

标签: python pandas python-applymap

我有以下数据框

ipdb> csv_data
  country_edited sale_edited  date_edited  transformation_edited
0          India      403171     20090101                     10
1         Bhutan      394096     20090101                     20
2          Nepal    Set Null     20090101                     30
3         madhya      355883     20090101                     40
4          sudan    Set Null     20090101                     50

我想将包含Set Null的所有列值替换为Nan,因此我接近以下方式

import numpy

def set_NaN(element):
    if element == 'Set Null':
        return numpy.nan
    else:
        return element

csv_data = csv_data.applymap(lambda element: set_NaN(element))

但它不会改变任何东西

ipdb> print csv_data
  country_edited sale_edited  date_edited  transformation_edited
0          India      403171     20090101                     10
1         Bhutan      394096     20090101                     20
2          Nepal    Set Null     20090101                     30
3         madhya      355883     20090101                     40
4          sudan    Set Null     20090101                     50
ipdb>

但是,如果我只打印csv_data.applymap(lambda element: set_NaN(element))如下所示,我可以看到输出,但当分配回来时,我无法获得我想要的数据

ipdb> csv_data.applymap(lambda element: set_NaN(element))
  country_edited sale_edited  date_edited  transformation_edited
0          India      403171     20090101                     10
1         Bhutan      394096     20090101                     20
2          Nepal         NaN     20090101                     30
3         madhya      355883     20090101                     40
4          sudan         NaN     20090101                     50

那么如何根据某些字符串用NaN替换列值?

1 个答案:

答案 0 :(得分:1)

您需要DataFrame.mask,它会将True掩码值替换为NaN。此外,有些列是数字的,因此首先需要dfstring的投射值:

print (csv_data.astype(str) == 'Set Null')
  country_edited sale_edited date_edited transformation_edited
0          False       False       False                 False
1          False       False       False                 False
2          False        True       False                 False
3          False       False       False                 False
4          False        True       False                 False


csv_data = csv_data.mask(csv_data.astype(str) == 'Set Null')
print (csv_data)
  country_edited sale_edited  date_edited  transformation_edited
0          India      403171     20090101                     10
1         Bhutan      394096     20090101                     20
2          Nepal         NaN     20090101                     30
3         madhya      355883     20090101                     40
4          sudan         NaN     20090101                     50

numpy boolean mask的另一个解决方案 - 比较DataFrame.values的numpy数组:

print (csv_data.values == 'Set Null')
[[False False False False]
 [False False False False]
 [False  True False False]
 [False False False False]
 [False  True False False]]

csv_data = csv_data.mask(csv_data.values == 'Set Null')
print (csv_data)
  country_edited sale_edited  date_edited  transformation_edited
0          India      403171     20090101                     10
1         Bhutan      394096     20090101                     20
2          Nepal         NaN     20090101                     30
3         madhya      355883     20090101                     40
4          sudan         NaN     20090101                     50

在您的解决方案中,必须将数据分配回csv_data

def set_NaN(element):
    if element == 'Set Null':
        return numpy.nan
    else:
        return element

csv_data = csv_data.applymap(lambda element: set_NaN(element))
print (csv_data)
  country_edited sale_edited  date_edited  transformation_edited
0          India      403171     20090101                     10
1         Bhutan      394096     20090101                     20
2          Nepal         NaN     20090101                     30
3         madhya      355883     20090101                     40
4          sudan         NaN     20090101                     50