2017-09-22 36 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.”字符串? –