In this section, we cover how to set the case of a string.
There are four common case settings:
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:
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.str.lower()
acts on one string column whose case we wish to set to lower case.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:
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()
.format()
for string interpolation.
See Interpolating.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:
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.str.upper()
acts on one string column whose case we wish to set to upper case.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
.Series
, we need to use
the str.upper()
method from core Python. For an example, see “Extension: Act on Literal”
under Lower above.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:
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.str.title()
acts on one string column whose case we wish to set to title case.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
.Series
, we need to use
the str.title()
method from core Python. For an example, see “Extension: Act on Literal”
under Lower above.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:
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.str.capitalize()
acts on one string column whose case we wish to set to sentence case.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
.Series
, we need to use
the str.capitalize()
method from core Python. For an example, see “Extension: Act on Literal”
under Lower Case above.