我認爲你需要dropna
用於去除NaN
S:
incoms=data['int_income'].dropna().unique().tolist()
print (incoms)
[75000.0, 50000.0, 0.0, 200000.0, 100000.0, 25000.0, 10000.0, 175000.0, 150000.0, 125000.0]
如果所有值都只能爲整數:
incoms=data['int_income'].dropna().astype(int).unique().tolist()
print (incoms)
[75000, 50000, 0, 200000, 100000, 25000, 10000, 175000, 150000, 125000]
,或者通過numpy.isnan
選擇所有非NaN值刪除NaN
S:
a = data['int_income'].unique()
incoms= a[~np.isnan(a)].tolist()
print (incoms)
[75000.0, 50000.0, 0.0, 200000.0, 100000.0, 25000.0, 10000.0, 175000.0, 150000.0, 125000.0]
a = data['int_income'].unique()
incoms= a[~np.isnan(a)].astype(int).tolist()
print (incoms)
[75000, 50000, 0, 200000, 100000, 25000, 10000, 175000, 150000, 125000]
純Python的解決方案 - slowier如果大DataFrame
:
incoms=[x for x in list(set(data['int_income'])) if pd.notnull(x)]
print (incoms)
[0.0, 100000.0, 200000.0, 25000.0, 125000.0, 50000.0, 10000.0, 150000.0, 175000.0, 75000.0]
incoms=[int(x) for x in list(set(data['int_income'])) if pd.notnull(x)]
print (incoms)
[0, 100000, 200000, 25000, 125000, 50000, 10000, 150000, 175000, 75000]
我想你需要['.dropna'(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna。 html) –
嘗試'pd.dropna()' – gobrewers14
如果我的(或其他)答案有幫助,請不要忘記[接受](http://meta.stackexchange.com/a/5235/295067)它 - 點擊答案旁邊的複選標記('✓')將其從灰色變爲灰色填充。謝謝。 – jezrael