YouTip LogoYouTip

Pandas Series Str Upper

[![Image 1: Pandas Common Functions](#) Pandas Common Functions](#) * * * `Series.str.upper()` is a function in Pandas used to convert strings to uppercase. During data processing, we often need to unify the case format of strings, such as converting abbreviations, titles, etc. to uppercase for easier reading and recognition. The `upper()` function can convert all strings in a Series to uppercase. **Word meaning**: `upper` means "higher, situated above", here it means converting letters to uppercase. * * * ## Basic Syntax and Parameters `str.upper()` is a string accessor method of Series, so you need to have a Series containing strings first, and then call it through the `.str` accessor. ### Syntax Format Series.str.upper() ### Parameter Description * **Parameters**: No parameters. This function does not require any parameters and can be called directly. ### Function Description * **Return value**: Returns a Series containing uppercase strings, the new Series has the same length as the original Series. * **Effect**: Converts each string element in the Series to uppercase, non-string elements remain unchanged. * **Note**: This function only converts letter characters, numbers, punctuation marks and other characters remain unchanged. * * * ## Examples Let's thoroughly master the usage of `str.upper()` through a series of examples from simple to complex. ### Example 1: Basic Usage - Converting Simple Strings ## Example import pandas as pd # Create a Series containing lowercase strings s = pd.Series(['hello','world','tutorial','python']) # Use str.upper() to convert all strings to uppercase result = s.str.upper() print("Original Series:") print(s) print("nConverted result:") print(result) **Output:** Original Series:0 hello 1 world 2 tutorial 3 python dtype: objectConverted result:0 HELLO 1 WORLD 2 TUTORIAL 3 PYTHON dtype: object **Code Analysis:** 1. `pd.Series(['hello', 'world', 'tutorial', 'python'])` creates a Series containing lowercase strings. 2. `s.str.upper()` calls the `upper()` method through the string accessor `.str` to convert all strings to uppercase. 3. The converted result is a new Series, the original Series remains unchanged. ### Example 2: Mixed Case String Conversion When the string contains mixed case characters, `upper()` will convert all lowercase letters to uppercase. ## Example import pandas as pd # Create a Series containing mixed case strings s = pd.Series(['Hello World','tutorial tutorial','PyThOn','Pandas']) # Use str.upper() to convert to uppercase result = s.str.upper() print("Original Series:") print(s) print("nConverted result:") print(result) **Output:** Original Series:0 Hello World1 tutorial tutorial 2 PyThOn3 Pandas dtype: objectConverted result:0 HELLO WORLD 1 TUTORIAL TUTORIAL 2 PYTHON 3 PANDAS **Code Analysis:** * No matter how many lowercase letters the string contains, `upper()` will convert them all to uppercase. * Spaces, numbers, and punctuation remain unchanged. * This is very useful when you need to unify data formats, such as converting country codes to uppercase. ### Example 3: Handling Strings with Special Characters `upper()` only converts letter characters, other characters remain unchanged. ## Example import pandas as pd # Create a Series containing special characters s = pd.Series(['hello123','world@tutorial','python!','a+b=c']) # Use str.upper() to convert to uppercase result = s.str.upper() print("Original Series:") print(s) print("nConverted result:") print(result) **Output:** Original Series:0 hello123 1 world@tutorial 2 python!3 a+b=c dtype: objectConverted result:0 HELLO123 1 WORLD@TUTORIAL 2 PYTHON!3 A+B=C dtype: object **Code Analysis:** * Numbers (123) remain unchanged. * Symbols (@, !, +, =) remain unchanged. * Only letter characters are converted to uppercase. ### Example 4: Standardizing Country Code Format In practical applications, `upper()` is often used to standardize country codes, currency codes, and other formats. ## Example import pandas as pd # User input country codes (may be in any case) country_codes = pd.Series(['cn','us','Uk','Jp','fr']) # Convert to uppercase uniformly standardized = country_codes.str.upper() print("Original country codes:") print(country_codes) print("nAfter standardization:") print(standardized) **Output:** Original country codes:0 cn 1 us 2 Uk3 Jp4 fr dtype: objectAfter standardization:0 CN 1 US 2 UK 3 JP 4 FR dtype: object * * * ## Notes * `str.upper()` only converts letter characters, numbers, spaces, punctuation marks and other non-letter characters remain unchanged. * If the Series contains non-string elements (such as NaN, integers, etc.), `upper()` will keep them as is and will not throw an error. * This function returns a new Series and does not modify the original data. * For Chinese character strings, Chinese characters are not affected because Chinese has no case distinction. * * Pandas Common Functions](#)
← Pandas Series Str ReplacePandas Series Cumsum β†’