【发布时间】:2017-09-22 17:34:02
【问题描述】:
我正在寻找 panda.core.series.Series 类的 max 值,当我使用以下代码时它返回 n.d.
rowMax = df.max(axis = 1)
问题: 什么是 n.d.意思是,我怎样才能得到一个实际值? (我的系列长度是20031)
【问题讨论】:
标签: python-3.x pandas series
我正在寻找 panda.core.series.Series 类的 max 值,当我使用以下代码时它返回 n.d.
rowMax = df.max(axis = 1)
问题: 什么是 n.d.意思是,我怎样才能得到一个实际值? (我的系列长度是20031)
【问题讨论】:
标签: python-3.x pandas series
我试着模拟你的问题:
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: int() 以 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
【讨论】:
如果它确实是一个系列而不是一个数据框,max 方法应该可以在没有任何参数的情况下工作。
s = pd.Series({'a' : 0., 'b' : 1., 'c' : 2.})
s.max()
> 2
你确定你不是在处理数据框吗?
【讨论】:
n.d. 值。我想更大的问题是处理这些行。关于如何跳过或忽略这些行的任何建议,或者我应该在 SO 上发布一个新问题吗?