用空白替换单元格值

时间:2019-08-21 00:01:43

标签: pandas python-3.7

我试图用空白值替换电子表格中的单元格值N/A

我尝试了各种在线上找到的replace格式。

if preferredphonenumber == 'N/A':
    replace = df1[rownum, 20].replace('N/A', 'blank', inplace=True, regex=True )
    print(replace)
else:
    print(df1)

我希望将N / A值替换为“空白”。

错误-

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2890, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: (8, 20)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/nickkeith2/Desktop/PBMS/PBMS/PBMS.py", line 17, in <module>
    replace = df1[rownum, 20].replace('N/A', 'blank', inplace=True, regex=True )
  File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 2975, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2892, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: (8, 20)

1 个答案:

答案 0 :(得分:0)

如果要将所有具有“ N / A”的单元格替换为“”,则可以使用

df.replace("N/A","", regex=True, inplace=True)

如果仅要将一列中的“ N / A”替换为“”,则可以使用

df['Column_Name'].replace("N/A","", regex=True, inplace=True)

如果只想在一个特定的单元格中将“ N / A”替换为“”,则可以使用

df.at['X', 'Y'] = 10 
df.set_value('X', 'Y', 10)
#in both of the above cases you are technically assigning a new value to the cell, X is row , Y is column