pshep123 pshep123 - 1 year ago 142
Python Question

Pandas Dataframe: set maximum value in column

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)


would yield:

0 10
1 12
2 15
3 15
4 15
5 15

But it doesn't.

There are a million solutions to find the maximum value, but nothing to set the maximum value... at least that I can find.

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

Thanks in advance.

Answer Source

I suppose you can do:

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download