Search code examples
pythonpandasfor-looplambda

python lambda function works in colab but not in jupyter for loop


Code doesn't work as intended in jupyter notebook and I have just run the update request but much of my environment was at the latest version already (so don't think it is a version) here is the code (that is suppose to remove spaces and commas from strings in the specific column);

for index, row in df.iterrows():
   row['Actors'] = [x.lower().replace(' ','') for x in row['Actors']]
   row['Director'] = ''.join(row['Director']).lower()

e.g. [Tim Robbins, Morgan Freeman, Bob Gunton]' goes to'[timrobbins, morganfreeman, bobgunton] jupyter executes the code with no errors reported but doesn't change the data. Colab runs perfect the first time.

The same is needed for director column however here there is only one name ; e.g. [Frank, Darabont] goes to frankdarabont (or should!)

I want to convert objects like director from a first name, last name to a joined firstnamelastname, similarly the actors, convert their full names into a single string (one for each actor)

Here is the dataframe.

The dataframe


Solution

  • I think you're looking for this:

    df['Actors'] = df['Actors'].str.lower().str.replace(r'(?<!,)\s', '', regex=True)
    df['Director'] = df['Director'].str.lower().str.replace(r'\s', '', regex=True)
    

    The result:

    enter image description here