Computer >> Computer tutorials >  >> Programming >> Python

Apply uppercase to a column in Pandas dataframe


Example

import pandas as pd
# A dataframe
df = pd.DataFrame({'Day': ['mon', 'tue', 'wed', 'thu', 'fri'],'Subject': ['Math', 'english', 'science', 'music', 'games']})
df['Day'] = df['Day'].str.upper()
print(df)

Output

Running the above code gives us the following result −

     Day       Subject
0    MON       Math
1    TUE       english
2    WED       science
3    THU       music
4    FRI       games

Example

# A dataframe
df = pd.DataFrame({'Day': ['mon', 'tue', 'wed', 'thu', 'fri'], 'Subject': ['Math', 'english', 'science', 'music', 'games']})
df1=df['Subject'].apply(lambda x: x.upper())
print(df1)

Output

Running the above code gives us the following result −

0       MATH
1    ENGLISH
2    SCIENCE
3      MUSIC
4      GAMES
Name: Subject, dtype: object