2017-09-22 38 views
0

我找了一類panda.core.series.Seriesmax值,並將其返回n.d.當我使用下面的代碼n.d.意思是在使用熊貓系列的python 3時?

rowMax = df.max(axis = 1) 

問:什麼日期不詳意味着什麼,我怎麼能得到一個實際的價值? (我的系列是20031的長度)

回答

1

我試圖模仿你的問題:

df = pd.DataFrame({'A':['1','3','4'], 
        'B':['5','6','3'], 
        'E':['3','4', 3]}) 

print (df) 
    A B E 
0 1 5 3 
1 3 6 4 
2 4 3 3 

a = df.max(axis=1) 
print (a) 
0 NaN 
1 NaN 
2 NaN 
dtype: float64 

這意味着你的數據屬於混合 - 數字處理字符串。

解決方案是所有的數據轉換成數字:

a = df.astype(int).max(axis=1) 
print (a) 
0 5 
1 6 
2 4 
dtype: int32 

有時它是沒有可能的,因爲非數值數據:

df = pd.DataFrame({'A':['rr','3','4'], 
        'B':['5','6','3'], 
        'E':['3','4', 3]}) 

print (df) 
    A B E 
0 rr 5 3 
1 3 6 4 
2 4 3 3 

a = df.astype(int).max(axis=1) 

ValueError: invalid literal for int() with base 10: 'rr'

然後可以使用to_numeric

a = df.apply(lambda x: pd.to_numeric(x, errors='coerce')) 
print (a) 
    A B E 
0 NaN 5 3 
1 3.0 6 4 
2 4.0 3 3 

a = df.apply(lambda x: pd.to_numeric(x, errors='coerce')).max(axis=1) 
print (a) 
0 5.0 
1 6.0 
2 4.0 
dtype: float64 
0

如果它確實是一個系列而不是數據框,max方法應該沒有任何爭論。

s = pd.Series({'a' : 0., 'b' : 1., 'c' : 2.}) 
s.max() 

> 2 

你確定你沒有處理數據幀嗎?

+0

這是一個系列,但我拿了仔細看看最初來自哪裏的數據幀,並且有很多行帶有'n.d.'值。我想更大的問題將是處理這些行。有關如何跳過或忽略這些行的任何建議,還是應該在SO上發佈新的問題? –

+0

該系列的dtype是什麼,是「n.d.」字符串? –