0

I am trying to write a for-loop in python on a pandas dataframe. I want to convert all strings in the color column that have "Green" as their last word (ex. Yellowish Green") to just "Green". Below is my loop:

for color in df['color']:
    low = color.split()
    if low[-1] == "Green":
        color = "Green"

However, this is not changing the actual values in the dataframe.

imad97
  • 224
  • 1
  • 8
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Feb 05 '23 at 04:38

3 Answers3

1

You can do:

def transform(string):
    last_word = string.split()[-1]
    if last_word.lower() == 'green':
        return 'green'
    else:
        return string

df['color'] = df['color'].apply(transform)
1

For loops in Python iterate over the values and not references of whatever you're iterating over. You could use list comprehensions instead:

df['color'] = ["Green" if x.split()[-1] == "Green" else x for x in df['color']]

Or if the word green isn't necessarily capitalized:

df['color'] = ["Green" if x.split()[-1].lower() == "green" else x for x in df['color']]
B Remmelzwaal
  • 1,581
  • 2
  • 4
  • 11
0

Using boolean indexing and string methods.

mask = df['Color'].str.endswith('Green')
df.loc[mask,'Color'] = 'Green'
wwii
  • 23,232
  • 7
  • 37
  • 77