Erste SchritteKostenlos loslegen

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

Diese Übung ist Teil des Kurses

Python for R Users

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung zum Anfassen

Probieren Sie diese Übung aus, indem Sie diesen Beispielcode ausführen.

# Convert sex to lower case
tips____ = tips____

# Convert smoker to upper case
tips____ = tips____

# Print the sex and smoker columns
print(tips[['sex', 'smoker']])
Bearbeiten und Ausführen von Code