从 Python Pandas 中的其他两个系列创建按元素的最小系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16989946/
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
Creating an element-wise minimum Series from two other Series in Python Pandas
提问by user2464433
I am having trouble finding a way to do an efficient element-wise minimum of two Series objects in pandas. For example I can add two Series easily enough:
我无法找到一种方法来对 Pandas 中的两个 Series 对象进行有效的元素最小化。例如,我可以很容易地添加两个系列:
In [1]:
import pandas as pd
s1 = pd.Series(data=[1,1,1], index=[1,2,3])
s2 = pd.Series(data=[1,2,2,1], index=[1,2,3,4])
s1.add(s2)
Out[1]:
1 2
2 3
3 3
4 NaN
dtype: float64
But I cannot find an efficient way to do an element-wise minimum between two Series (along with aligning the indices and handling NaN values).
但是我找不到一种有效的方法来在两个系列之间进行元素最小化(以及对齐索引和处理 NaN 值)。
Nevermind. There is an escape hatch with the combine function so you can put in any element-wise function:
没关系。有一个带有组合功能的逃生舱口,因此您可以放入任何元素明智的功能:
In [2]:
s1 = pd.Series(data=[1,1,1], index=[1,2,3])
s2 = pd.Series(data=[1,2,2,1], index=[1,2,3,4])
s1.combine(s2, min, 0)
Out[2]:
1 1
2 1
3 1
4 0
dtype: int64
回答by BrenBarn
The most straightforward way I can see is to make them into a DataFrame and then take the row-wise min:
我能看到的最直接的方法是将它们变成一个 DataFrame,然后按行取最小值:
>>> print pandas.concat([s1, s2], axis=1).min(axis=1)
1 1
2 1
3 1
4 1
dtype: float64
回答by Andy Hayden
Another similar way:
另一种类似的方式:
In [11]: pd.DataFrame([s1, s2]).min()
Out[11]:
1 1
2 1
3 1
4 1
dtype: float64
回答by Ankur Kanoria
I find this the simplest:
我觉得这是最简单的:
import numpy as np
smax = np.minimum(s1, s2)
回答by Martin Thoma
Same answer as Andy Hayden, but a bit easier to read:
与安迪海登相同的答案,但更容易阅读:
>>> import pandas as pd
>>> s1 = pd.Series(data=[1,2,3,4,5], index=[1,2,3,4,5])
>>> s2 = pd.Series(data=[5,1,3,5], index=[1,2,3,4])
>>> pd.DataFrame([s1, s2]).min()
1 1.0
2 1.0
3 3.0
4 4.0
5 5.0
dtype: float64
回答by frenzykryger
pd.Series([1,2,3,4,5]).clip(upper=pd.Series([6,5,4,3,2]))
will get you:
会让你:
0 1
1 2
2 3
3 3
4 2
dtype: int64
回答by shaneb
You can use the combinemethod of a DataFrame with np.minimumas the argument. np.minimumhas special handling for NaN and complex NaNs.
您可以使用combineDataFrame的方法np.minimum作为参数。 np.minimum对 NaN 和复杂的 NaN 有特殊处理。
Indeed, the pandas docs for combineuses the np.minimumfunction to illustrate a "true element-wise combine":
事实上,用于组合的熊猫文档使用该np.minimum函数来说明“真正的元素组合”:
>>> df1 = pd.DataFrame({'A': [5, 0], 'B': [2, 4]})
>>> df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]})
>>> df1.combine(df2, np.minimum)
A B
0 1 2
1 0 3
回答by renan-eccel
This method will do the job:
此方法将完成以下工作:
import pandas as pd
def elementwise_min(x, y):
x[x > y] = y
return x
a = pd.Series([1, 2, 3])
b = pd.Series([0, 2, 4])
elementwise_min(a, b)
回答by shaneb
You can concat the dataframes and take the minimum, specifying level=0:
您可以连接数据帧并取最小值,指定level=0:
>>> s1 = pd.Series(data=[1,1,1], index=[1,2,3])
>>> s2 = pd.Series(data=[1,2,2,1], index=[1,2,3,4])
>>> pd.concat([s1, s2]).min(level=0)
1 1
2 1
3 1
4 1
dtype: int64
This approach also works on dataframes.
这种方法也适用于数据帧。

