Iterrate over values in a Pandas Series to replace a value if needed

Viewed 701

I am trying to iterate through a Pandas Series.

If a value in the column '% Renewable' is greater than the median for the '% Renewable' column, the value should be assigned to 1 in a new column 'HighRenew'. Otherwise the value should remain as it is.

The value of each item in the new series 'HighRenew' should change according to whether the condition is met.

for value in Reducedset['% Renewable']: 
    if value >= Reducedset['% Renewable'].median():
       Reducedset['HighRenew'] = 1 
    else:
       Reducedset['HighRenew'] = value       

However, I only want to amend the specific value/element in the series and not the entire series when the condition is met by the if loop.

Could anybody give me any advice?

3 Answers

You could use boolean variables instead.

median_value = Reducedset['% Renewable'].median()
Reducedset['% Renewable'][Reducedset['% Renewable'] > median_value] = 1

IIUC you want to create a new column (or modify an existing column) to get the value from another column if that value is less than the median or 1 if the value if greater or equal. It can be done simply:

Reducedset['HighRenew'] = Reducedset['% Renewable']
Reducetset.loc[Reducedset['% Renewable'] < Reducedset['% Renewable'].median(), 'HighRenew'] = 1

Alternatively you could use np.where to do that in one single pass:

Reducedset['HighRenew'] = np.where(Reducedset['% Renewable'] >= Reducedset['% Renewable'].median(),
                                   1, Reducedset['% Renewable'])

You can do something like this

# The median can be calculated just once
median = Reducedset["% Renewable"].median()

for idx,row in Reducedset.iterrows():
    value = row["% Renewable"]
    if value >= median:
        df.iloc[idx,"% Renewable"] = 1
    # No need to re-assing the value if the condition is not met

I am assuming that Reduceset is your pandas.dataframe. Also, I am calculating the median before entering the loop, otherwise, the median will be changing every time the condition inside the loop is met.

Hope this helps, let me know how does it go! :D

Related