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()
2