DataFrame apply filtering by other column

Viewed 697

Can I make an apply of a function to a dataframe column by filtering rows with another column value?

My actual code is:

df['description_text_clean'] = df.description_text_clean[df['language']!='en'].apply(translate_to_en)

Where I am trying to filter the column of language by the ones that aren't english and I want to translate the description_text_clean column. But in the end I obtain that the description_text_clean column have been modified in the english columns by NaN and I want them to continue as they were.

Example DataFrame:

df = pd.DataFrame([['Example text', 'en'], 
                   ['No es ingles', 'es'], 
                   ['I am again english', 'en']], 
                   columns=['description_text_clean', 'language'])

Actual result:

    description_text_clean  language
0   NaN                       en
1   It is not English         es
2   NaN                       en

The desired result will be to obtain a dataframe with all the texts translated to english with the translate_to_en function that uses google translate api:

    description_text_clean  language
0   Example text              en
1   It is not English         es
2   I am again english        en

Any suggestion?

1 Answers

My bad i missed a small detail in my comments, try with .loc[],series.mask or series.combine_first or np.where

df.loc[df['language'].ne('en'),'description_text_clean']  = (
                     df['description_text_clean'].map(fun))

Or:

(df.loc[df['language'].ne('en'),'description_text_clean'].map(translate_to_en)
  .combine_first(df.description_text_clean))

Or:

df['description_text_clean'].mask(df['language'].ne('en')
                                  ,df['description_text_clean'].map(translate_to_en))

Or even np.where:

np.where(df['language'].ne('en'),
        df['description_text_clean'].map(fun),df['description_text_clean'])

Tested with dummy function:

def fun(x):
    return  1
df['description_text_clean'] =(df.loc[df['language'].ne('en'),'description_text_clean']
                      .map(fun).combine_first(df.description_text_clean))

Or:

df['description_text_clean'] = df['description_text_clean'].mask(df['language'].ne('en')
                              ,df['description_text_clean'].map(fun))
print(df)

  description_text_clean language
0           Example text       en
1                      1       es
2     I am again english       en
Related