Python Pandas -- Concatenate strings if condition

concatenation, pandas, python

Solution

You can try:

df['A'] = df.A.apply(lambda s: '7' + s if len(s) == 5 else s) 

Problem

In a `pd.DataFrame` I have strings in column `A` whose `len` is equal to 5 or 6. If the cell value `len(value) == 5` then I need to concatenate the string `'7'` to the beginning of the cell value. I am currently trying the code below, but cannot seem to use apply with the `inplace=True` argument. ``` df[df['A'].str.len() == 5]['A'].apply(lambda x: '7' + str(x)) ``` this will return the column - but won't modify the dataframe inplace...

Original source