Strings
Columns containing strings are represented as the object type in Pandas.
Since a lot of data you will encounter involve strings, it is important that you know how to manipulate them.
Python allows you to use its built-in string manipulation methods with the str accessor. There are several string methods, some of which are .upper() and .lower(). They convert strings to upper and lower case, respectively.
# Converts 'col_a' to lower case
df['col_a'].str.lower()
# Converts 'col_b' to upper case
df['col_b'].str.upper()
Cet exercice fait partie du cours
Python for R Users
Instructions
- Inspect the
'sex'and'smoker'columns in the shell. - Convert the
'sex'column into lower case. - Convert the
'smoker'column into upper case. - Inspect the
'sex'and'smoker'columns to ensure that case conversion worked.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Convert sex to lower case
tips____ = tips____
# Convert smoker to upper case
tips____ = tips____
# Print the sex and smoker columns
print(tips[['sex', 'smoker']])