In a pandas DataFrame, the columns property represents the column names of the DataFrame.
df.columns = df.columns.str.strip().str.upper()
Above snippet convert all the column names to upper case.
string_methods_on_column_names.py
import pandas as pd
# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'kranthi', 'Jitendra', 'Kumar'],
'Hobbies': ['Football,Cricket', 'Tennis, cricket', 'Trekking, reading books', 'Chess', 'Read Books', 'Cricket']}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)
df.columns = df.columns.str.upper()
print('\nConverted column to upper case :\n', df)
Output
Original DataFrame Name Hobbies 0 Krishna Football,Cricket 1 Sailu Tennis, cricket 2 Joel Trekking, reading books 3 kranthi Chess 4 Jitendra Read Books 5 Kumar Cricket Converted column to upper case : NAME HOBBIES 0 Krishna Football,Cricket 1 Sailu Tennis, cricket 2 Joel Trekking, reading books 3 kranthi Chess 4 Jitendra Read Books 5 Kumar Cricket
Previous Next Home
No comments:
Post a Comment