2
我有下面的數據幀的字符串取代基於在熊貓
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
那麼如何更換基於特定字符串楠列值的數據?
你這裏得到什麼:'csv_data = csv_data.applymap(set_NaN)'。另外,'applymap'需要一個可調用的,所以在這裏額外的'lambda'是不必要的。 –
好吧,我不知道,我是熊貓新手 –