pythondataframe

How to apply the capitalize with condition?


I'm wondering how to use the capitalize function when another column has a specific value.
For example, I want to change the first letter of students with Master's degree.

# importing pandas as pd 
import pandas as pd 

# creating a dataframe 
df = pd.DataFrame({
    'A': ['john', 'bODAY', 'minA', 'peter', 'nicky'], 
    'B': ['Masters', 'Graduate', 'Graduate', 'Masters', 'Graduate'], 
    'C': [27, 23, 21, 23, 24]
})

# Expected result
#   A   B   C
#0  John    Masters 27
#1  bODAY   Graduate    23
#2  minA    Graduate    21
#3  Peter   Masters 23
#4  nicky   Graduate    24

I tried it like this, but it didn't apply well.

df[df['B']=='Masters']['A'].str = df[df['B']=='Masters']['A'].str.capitalize()

Solution

  • Here is the complete code:

    import pandas as pd 
    
    # Creating the DataFrame
    df = pd.DataFrame({
        'A': ['john', 'bODAY', 'minA', 'peter', 'nicky'], 
        'B': ['Masters', 'Graduate', 'Graduate', 'Masters', 'Graduate'], 
        'C': [27, 23, 21, 23, 24]
    })
    
    # Capitalize column A conditionally based on B
    df['A'] = df.apply(lambda row: row['A'].capitalize() if row['B'] == 'Masters' else row['A'], axis=1)
    
    # Display the updated DataFrame
    print(df)
    

    Output:

         A         B   C
    0   John   Masters  27
    1  bODAY  Graduate  23
    2   minA  Graduate  21
    3  Peter   Masters  23
    4  nicky  Graduate  24