Case

In this section, we cover how to set the case of a string.

There are four common case settings:

  1. Lower where all characters are lower case e.g. "lower".
  2. Upper where all characters are upper case e.g. "Upper".
  3. Title where the first character of each word is upper case and the rest is lower case e.g. " String Formatting".
  4. Sentence where the first character of the first word is upper case and the rest is lower case e.g. "String formatting is cool.".

To Lower

We wish to set the case of a string column to lower case.

In this example, we wish to set the case of all the individual string values in the string column col_1 to lower case.

df_2 = df.assign(
    col_2 = df['col_1'].str.lower()
)

Here is how this works:

  • We use the str.lower() method from the str accessor set of string manipulation methods of Pandas Series to set the case of all letters in each string element in the column col_1 to lower case.
  • The str.lower() acts on one string column whose case we wish to set to lower case.
  • The output data frame df_2 will have the same number of rows as the original data frame df but with an additional columncol_2 holding an all lower case version of the column col_1.

Extension: Act on Literal

We wish to set the case of a string literal (individual value) to lower case.

In this example, we wish to interpolate the all lower case version of the string column col_1 in to the template ‘{} ago’ where the value of col_1 is inserted in the place of the ‘{}’.

df_2 = df.assign(
    col_2 = df['col_1'].apply(
        lambda x: '{} ago'.format(x.lower())
    )
)

Here is how this works:

  • In this case we need to act on each string value individually and when we need to act on an individual string value, we can’t use the str.lower() method (or any of the str accessor methods). In that case, we need to use the core Python counterpart which is str.lower().
  • We use the function format() for string interpolation. See Interpolating.

To Upper

We wish to set the case of a string column to upper case.

In this example, we wish to set the case of all the individual string values in the string column col_1 to upper case.

df_2 = df.assign(
    col_2 = df['col_1'].str.upper()
)

Here is how this works:

  • We use the str.upper() method from the str accessor set of string manipulation methods of Pandas Series to set the case of all letters in each string element in the column col_1 to upper case.
  • The str.upper() acts on one string column whose case we wish to set to upper case.
  • The output data frame df_2 will have the same number of rows as the original data frame df but with an additional columncol_2 holding an all upper case version of the column col_1.
  • If we need to act on an individual string literal, as opposed to a Series, we need to use the str.upper() method from core Python. For an example, see “Extension: Act on Literal” under Lower above.

To Title

We wish to set the case of a string column (or literal) to title capitalization. Title capitalization is where the first character of each word is upper case and the rest is lower case e.g. "String Formatting".

In this example, we wish to set the case of all the individual string values in the string column col_1 to title case.

df_2 = df.assign(
    col_2 = df['col_1'].str.title()
)

Here is how this works:

  • We use the str.title() method from the str accessor set of string manipulation methods of Pandas Series to set the case of all letters in each string element in the column col_1 to title case.
  • The str.title() acts on one string column whose case we wish to set to title case.
  • The output data frame df_2 will have the same number of rows as the original data frame df but with an additional columncol_2 holding an all title case version of the column col_1.
  • If we need to act on an individual string literal, as opposed to a Series, we need to use the str.title() method from core Python. For an example, see “Extension: Act on Literal” under Lower above.

To Sentence

We wish to set the case of a string column (or literal) to sentence capitalization. Sentence capitalization is where the first character of the first word is upper case and the rest is lower case e.g. "String formatting is cool.".

In this example, we wish to set the case of all the individual string values in the string column col_1 to title case.

df_2 = df.assign(
    col_2 = df['col_1'].str.capitalize()
) 

Here is how this works:

  • We use the str.capitalize() method from the str accessor set of string manipulation methods of Pandas Series to set the case of all letters in each string element in the column col_1 to sentence case.
  • The str.capitalize() acts on one string column whose case we wish to set to sentence case.
  • The output data frame df_2 will have the same number of rows as the original data frame df but with an additional columncol_2 holding an all title sentence version of the column col_1.
  • If we need to act on an individual string literal, as opposed to a Series, we need to use the str.capitalize() method from core Python. For an example, see “Extension: Act on Literal” under Lower Case above.
PYTHON
I/O