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

Convert String into lower, upper or camel case in Pandas

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).

Convert String Case in Pandas

1. Setup:

Ensure you have pandas installed:

pip install pandas

2. Import Necessary Libraries:

import pandas as pd

3. Create a Sample DataFrame:

df = pd.DataFrame({
    'Text': ['Hello WORLD', 'pandas IS great', 'PYThon is Fun']
})
print(df)

4. Convert to Lowercase:

Using the .str accessor, you can quickly transform strings in a Series to lowercase:

df['Text_Lower'] = df['Text'].str.lower()
print(df)

5. Convert to Uppercase:

Similarly, you can convert strings to uppercase:

df['Text_Upper'] = df['Text'].str.upper()
print(df)

6. Convert to Camel Case (or Title Case):

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)

7. (Bonus) Convert to Proper CamelCase:

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)

8. Summary:

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.

  1. Changing string case in Pandas Series:

    • Description: Use the .str accessor in Pandas to change the case of strings in a Series.
    • Code:
      import pandas as pd
      
      # Sample Series
      data = pd.Series(['apple', 'Orange', 'Banana'])
      
      # Convert strings to lowercase
      result = data.str.lower()
      
  2. Convert string to upper case in Pandas DataFrame:

    • Description: Apply the .str.upper() method to convert strings to uppercase in a Pandas DataFrame.
    • Code:
      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()
      
  3. Pandas Series string case conversion methods:

    • Description: Use various string case conversion methods available in Pandas.
    • Code:
      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()
      
  4. Title case conversion in Pandas for strings:

    • Description: Use the .str.title() method to convert strings to title case (capitalize first letter of each word).
    • Code:
      import pandas as pd
      
      # Sample Series
      data = pd.Series(['apple pie', 'orange juice', 'banana split'])
      
      # Convert strings to title case
      result = data.str.title()
      
  5. Convert string to camel case in Pandas:

    • Description: Implement a custom function or use libraries like inflect to convert strings to camel case.
    • Code:
      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))
      
  6. Applying str.lower() to Pandas Series:

    • Description: Use the str.lower() method directly on a Pandas Series to convert strings to lowercase.
    • Code:
      import pandas as pd
      
      # Sample Series
      data = pd.Series(['Apple', 'Orange', 'Banana'])
      
      # Convert strings to lowercase using str.lower()
      result = data.str.lower()
      
  7. String case transformation using Pandas apply():

    • Description: Apply a custom function using the apply() method for more complex string case transformations.
    • Code:
      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)
      
  8. Pandas str.upper() method for string conversion:

    • Description: Utilize the str.upper() method to convert strings to uppercase in Pandas.
    • Code:
      import pandas as pd
      
      # Sample Series
      data = pd.Series(['apple', 'Orange', 'Banana'])
      
      # Convert strings to uppercase using str.upper()
      result = data.str.upper()
      
  9. Camel case conversion with Pandas Series in Python:

    • Description: Implement a custom function or use libraries like inflect to convert strings to camel case.
    • Code:
      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))