簡體   English   中英

Python/Pandas — 將一列中的 NaN 替換為空/空白,另一列替換為 0。是否可以選擇性地替換 Nan?

[英]Python/Pandas —replace NaN in a column with empty/blank, another column with 0. Is it possible to selectively replace Nan?

嗨,剛開始學習 Python 或編程本身,並嘗試從 Excel 中讀取 DataFrame 並將其插入電子郵件中。 字符串列中有一個空白,我想用空白替換 Nan。 在其他地方我有另一個 int 列我想格式化為 {:1f} 但它有時也包括 NaN 因為我使用 =IFERROR(...,""),那是格式化返回錯誤的時候。 除了將 excel 中的公式固定為 =IFERROR(...,0) 之外,我還能如何解決這個問題?

閱讀工作表時,請在pd.read_excel函數中使用轉換器。

pd.read_excel('sheet.xlsx', sheet_name='bad', 
              converters={'string_col': lambda x: '' if pd.isna(x) else x,
                          'int_col': lambda x: x if pd.isna(x) else round(x,1)
              }
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM