Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
String manipulation is an essential aspect of data cleaning and preprocessing in pandas. In this tutorial, we'll go over how to convert strings in a pandas DataFrame or Series to lowercase, uppercase, and camel case (or title case).
Ensure you have pandas installed:
pip install pandas
import pandas as pd
df = pd.DataFrame({ 'Text': ['Hello WORLD', 'pandas IS great', 'PYThon is Fun'] }) print(df)
Using the .str
accessor, you can quickly transform strings in a Series to lowercase:
df['Text_Lower'] = df['Text'].str.lower() print(df)
Similarly, you can convert strings to uppercase:
df['Text_Upper'] = df['Text'].str.upper() print(df)
Pandas doesn't use the term "camel case" but refers to it as "title case", where each word's first letter is capitalized:
df['Text_Title'] = df['Text'].str.title() print(df)
If you need to convert strings to CamelCase without spaces (e.g., "HelloWorld"), it's a bit more involved. Here's one way to do it using a lambda function:
df['Text_CamelCase'] = df['Text'].apply(lambda x: ''.join(word.capitalize() for word in x.split())) print(df)
Pandas provides easy-to-use string methods via the .str
accessor, allowing for efficient manipulation of text data in a DataFrame or Series. Whether you need to standardize case in a dataset or convert between different naming conventions, pandas has got you covered.
Changing string case in Pandas Series:
.str
accessor in Pandas to change the case of strings in a Series.import pandas as pd # Sample Series data = pd.Series(['apple', 'Orange', 'Banana']) # Convert strings to lowercase result = data.str.lower()
Convert string to upper case in Pandas DataFrame:
.str.upper()
method to convert strings to uppercase in a Pandas DataFrame.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': ['apple', 'orange', 'banana']}) # Convert strings in column 'A' to uppercase df['A'] = df['A'].str.upper()
Pandas Series string case conversion methods:
import pandas as pd # Sample Series data = pd.Series(['apple', 'Orange', 'Banana']) # Convert to lowercase, uppercase, and title case lowercase = data.str.lower() uppercase = data.str.upper() titlecase = data.str.title()
Title case conversion in Pandas for strings:
.str.title()
method to convert strings to title case (capitalize first letter of each word).import pandas as pd # Sample Series data = pd.Series(['apple pie', 'orange juice', 'banana split']) # Convert strings to title case result = data.str.title()
Convert string to camel case in Pandas:
inflect
to convert strings to camel case.import pandas as pd import inflect # Sample Series data = pd.Series(['hello world', 'good morning', 'happy new year']) # Convert strings to camel case using inflect p = inflect.engine() camel_case = data.apply(lambda x: p.camelize(x))
Applying str.lower() to Pandas Series:
str.lower()
method directly on a Pandas Series to convert strings to lowercase.import pandas as pd # Sample Series data = pd.Series(['Apple', 'Orange', 'Banana']) # Convert strings to lowercase using str.lower() result = data.str.lower()
String case transformation using Pandas apply():
apply()
method for more complex string case transformations.import pandas as pd # Sample Series data = pd.Series(['apple', 'Orange', 'Banana']) # Custom function to convert to lowercase and add ' fruit' def transform_case(value): return value.lower() + ' fruit' # Apply the custom function result = data.apply(transform_case)
Pandas str.upper() method for string conversion:
str.upper()
method to convert strings to uppercase in Pandas.import pandas as pd # Sample Series data = pd.Series(['apple', 'Orange', 'Banana']) # Convert strings to uppercase using str.upper() result = data.str.upper()
Camel case conversion with Pandas Series in Python:
inflect
to convert strings to camel case.import pandas as pd import inflect # Sample Series data = pd.Series(['hello world', 'good morning', 'happy new year']) # Convert strings to camel case using inflect p = inflect.engine() camel_case = data.apply(lambda x: p.camelize(x))