pandas 从不同的列中取绝对值的最大值并过滤掉 NaN Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34131285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:20:18 来源:igfitidea点击:
Take the maximum in absolute value from different columns and filter out NaN Python
提问by gis20
This was my try. For example
这是我的尝试。例如
df = pd.DataFrame({'a':[5,0,1,np.nan], 'b':[np.nan,1,4,3], 'c':[-3,-2,0,0]})
df.dropna(axis=1).max(axis=1,key=abs)
Filters out well the NaN
values but it gets 0 or negative values instead of the highes in absolute value
很好地过滤掉NaN
值,但它得到 0 或负值而不是绝对值的最高值
The result should be one column with
结果应该是一列
5
-2
4
3
回答by gis20
I solved by
我解决了
maxCol=lambda x: max(x.min(), x.max(), key=abs)
df.apply(maxCol,axis=1)
回答by thomas
You can use np.nanargmax
on the squared data:
您可以np.nanargmax
在平方数据上使用:
>>> df.values[range(df.shape[0]),np.nanargmax(df**2,axis=1)]
array([ 5., -2., 4., 3.])
回答by Anton Protopopov
df = df.fillna(0)
l = df.abs().values.argmax(axis=1)
pd.Series([df.values[i][l[i]] for i in range(len(df.values))])
In [532]: pd.Series([df.values[i][l[i]] for i in range(len(df.values))])
Out[532]:
0 5
1 -2
2 4
3 3
dtype: float64
One liner:
一个班轮:
pd.Series([df.values[i][df.fillna(0).abs().values.argmax(axis=1)[i]] for i in range(len(df.values))])