我試圖運行我認爲簡單的代碼來消除所有NaN中的任何列,但無法使其正常工作(axis = 1
在刪除行時工作正常) :熊貓:IndexingError:不可對齊的布爾系列作爲索引器提供
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,np.nan,np.nan], 'b':[4,np.nan,6,np.nan], 'c':[np.nan, 8,9,np.nan], 'd':[np.nan,np.nan,np.nan,np.nan]})
df = df[df.notnull().any(axis = 0)]
print df
完整的錯誤:
raise IndexingError('Unalignable boolean Series provided as 'pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match
預期輸出:
a b c
0 1.0 4.0 NaN
1 2.0 NaN 8.0
2 NaN 6.0 9.0
3 NaN NaN NaN
Ahhhh,因爲'df []'方法正在尋找一個基於行的索引,而不是一個基於列的索引。收到了。謝謝。 – pshep123
@ pshep123 - 很高興能幫到你! – jezrael