pandas 求min和max等函数,pandas系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27473975/
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-13 22:45:56  来源:igfitidea点击:

Finding min and max and other functions, pandas series

pythonnumpypandas

提问by user3659451

Let ssbe a pandas series of floats.

让我们ss成为一个Pandas系列的花车。

Let xbe a float.

让我们x成为一个浮动。

x+1and ss+1work as expected.

x+1并按ss+1预期工作。

x/5and ss/5work as expected.

x/5并按ss/5预期工作。

However min(x,5)works, but in order to get the same behavior with the series I must do ss.apply(lambda y: min(y,6))and this is a pain for the following reason:

但是min(x,5)有效,但为了获得与该系列相同的行为,我必须这样做ss.apply(lambda y: min(y,6)),由于以下原因,这很痛苦:

What if I want to do a batch of calculations on a series, and then as new elements appear, I want to do on the fly calculations?

如果我想对一个系列进行一批计算,然后当新元素出现时,我想进行动态计算怎么办?

In other words I'd like a function fso that I can apply fto a series (or multiple series) as well as applying fto a float (or multiple floats), and ideally apply fto a combination of floats and series.

换句话说,我想要一个函数,f以便我可以应用于f一个系列(或多个系列)以及应用于f一个浮点数(或多个浮点数),并且理想地适用f于浮点数和系列的组合。

I care about this because there may be many such transformations and I would highly prefer not maintaining two different versions of each transformation, one for batches and one for instant one-offs.

我关心这一点,因为可能有很多这样的转换,我非常不想为每个转换维护两个不同的版本,一个用于批量,一个用于即时一次性。

回答by orange

If you have a scalar, you can either convert it to a Series and work on the Series

如果您有标量,则可以将其转换为系列并处理系列

import pandas as pd
s = pd.Series(range(10, 20))
print s.min()
f = 6
print s.append(pd.Series([f])).min()

or convert the Series to a scalar value and use the normal min()function

或将系列转换为标量值并使用普通min()函数

print min(s.min(), f)