在pandas DataFrame中设置最大值(上限) [英] Set maximum value (upper bound) in pandas DataFrame
本文介绍了在pandas DataFrame中设置最大值(上限)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试设置pandas DataFrame列的最大值.例如:
I'm trying to set a maximum value of a pandas DataFrame column. For example:
my_dict = {'a':[10,12,15,17,19,20]}
df = pd.DataFrame(my_dict)
df['a'].set_max(15)
将产生产量:
a
0 10
1 12
2 15
3 15
4 15
5 15
但事实并非如此.
有一百万种解决方案,以找到最大值,但没有任何办法设置最大值……至少我能找到.
There are a million solutions to find the maximum value, but nothing to set the maximum value... at least that I can find.
我可以遍历列表,但是我怀疑有一种更快的方法可以处理大熊猫.我的列表将明显更长,因此我希望迭代花费相对较长的时间.另外,我希望能够使用NaN
的任何解决方案.
I could iterate through the list, but I suspect there is a faster way to do it with pandas. My lists will be significantly longer and thus I would expect iteration to take relatively longer amount of time. Also, I'd like whatever solution to be able to handle NaN
.
推荐答案
我想您可以做到:
maxVal = 15
df['a'].where(df['a'] <= maxVal, maxVal) # where replace values with other when the
# condition is not satisfied
#0 10
#1 12
#2 15
#3 15
#4 15
#5 15
#Name: a, dtype: int64
或者:
df['a'][df['a'] >= maxVal] = maxVal
这篇关于在pandas DataFrame中设置最大值(上限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文