'upper()'
method converts the Strings in given column to upper case and return a new
series with the updated data.
Example
df['Name'] = df['Name'].str.upper()
As you see above snippet, we are using 'str' accessor to perform the string operations
Find the below working application.
upper.py
import pandas as pd
# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Krishna"],
'Age': [34, 35, 234, 35, 52, 34],
'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
'Rating': [67, 43, 67, 100, 41, 89]}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)
df['Name'] = df['Name'].str.upper()
df['City'] = df['City'].str.upper()
df['Gender'] = df['Gender'].str.upper()
print('\nDataFrame after converting the columns data to upper case \n', df)
Output
Original DataFrame Name Age City Gender Rating 0 Krishna 34 Bangalore Male 67 1 Sailu 35 Hyderabad Female 43 2 Joel 234 Hyderabad Male 67 3 Chamu 35 Chennai Female 100 4 Jitendra 52 Bangalore Male 41 5 Krishna 34 Chennai Male 89 DataFrame after converting the columns data to upper case Name Age City Gender Rating 0 KRISHNA 34 BANGALORE MALE 67 1 SAILU 35 HYDERABAD FEMALE 43 2 JOEL 234 HYDERABAD MALE 67 3 CHAMU 35 CHENNAI FEMALE 100 4 JITENDRA 52 BANGALORE MALE 41 5 KRISHNA 34 CHENNAI MALE 89
No comments:
Post a Comment