我有一個熊貓數據框有46列和6行。pandas更新數據幀的值,如果單元格包含' - '
Index Column1 Column2 Column3 Column4 ... # Cant type all 46 columns.
2012 5626 fooo - barrr
2013 5655h booo - barr
2014 5626d zooo - -
LTM 56 gooo greed -
有沒有辦法對我來說,通過這個數據幀,並更新所有-
值是0
或null
值?
我曾嘗試:
for zzz in df.columns: # since df.columns will return me the names of the columns
if df_final[zzz].any() == '-':
df_final[zzz] = 0
print(df_final)
然而,這只是打印出來的一切,因爲它是。它不會轉換成-
0/null
即使你的代碼可以工作,它的錯誤語義也是如此,因爲它會將整列更新爲'0',並且未能顯示代碼段中的'df_final' – EdChum
@jake,建議:避免使用循環。避免編碼你的數據框在循環中工作,這不會得到你的結果。 Python有不同的處理名稱的方式,而且事情從任何地方都不可變。熊貓幾乎總是會返回一份副本,所以任何更改都不會影響父項。使用DataFrame方法。閱讀關於如何做到這一點的熊貓文檔。 – Kartik