avatarThat's it ! Code Snippets

Summary

This article provides a guide on how to process a column as a string in Pandas, a popular data manipulation library in Python.

Abstract

The article begins by explaining how to convert all strings in a column to lower or upper case using the lower() and upper() methods, respectively. It then demonstrates how to remove whitespace at the end, beginning, or both ends of all strings in a column using the rstrip(), lstrip(), and strip() methods, respectively. The article also shows how to capitalize the first letter of each string in the series using the str.capitalize method and how to capitalize each word’s first letter in the series using the str.title method. Additionally, the article explains how to slice a substring from a string in the column series using the str.slice method or Python’s list-slicing syntax. Finally, the article demonstrates how to check if a string is contained in all column values using the str.contains method and how to check if all values in a column start with a string using the str.startswith method. The article concludes by explaining how to split a string in a column using the str.split method.

Bullet points

  • Convert all strings in a column to lower case using the lower() method.
  • Convert all strings in a column to upper case using the upper() method.
  • Remove whitespace at the end of all strings in a column using the rstrip() method.
  • Remove whitespace at the beginning of all strings in a column using the lstrip() method.
  • Remove whitespace at the beginning and end of all strings in a column using the strip() method.
  • Capitalize the first letter of each string in the series using the str.capitalize method.
  • Capitalize each word’s first letter in the series using the str.title method.
  • Slice a substring from a string in the column series using the str.slice method or Python’s list-slicing syntax.
  • Check if a string is contained in all column values using the str.contains method.
  • Check if all values in a column start with a string using the str.startswith method.
  • Split a string in a column using the str.split method.

Pandas >> How to process a Column as a String in Pandas

In this article, we will talk about how to treat a whole column as a string.

Firstly, we will prepare the data for demonstration.

How to convert all strings of a column to lower case

We can use lower() method of str property of the column to convert the value of the column to lower case.

How to convert all strings of a column to upper case

We can use upper() method of str property of the column to convert the value of the column to upper case.

How to remove whitespace at the end of all strings in a column

We can use rstrip() method of str property of the column to remove whitespace at the end of a string in a column.

How to remove whitespace at the beginning of all strings in a column

We can use lstrip() method of str property of the column to remove whitespace at the beginning of a string in a column.

How to remove whitespace at the beginning and end of all strings in a column

We can use strip() method of str property of the column to remove whitespace at the beginning and end of a string in a column.

How to capitalize the first letter of each string in the Series

We can use str.capitalize method to capitalize the first letter of each string in the Series.

How to capitalize each word’s first letter in the Series

We can use str.title to capitalize each word’s first letter in the Series.

How to slice a substring from string in the column Series

We can use str.slice to extract a substring from a string in the column Series.

Of course, you can also replace the slice method with Python’s list-slicing syntax.

How to check if a string is contained in all column values

We can use str.contains to check every string in a column if it contains a string. The boolean result can be used to filter rows that meet the condition.

How to check if all values in column starts with a string

We can use str.startswith to check every string in a column if it starts with a string.

How to split string in column

We can use str.split to split every string in a column. A list will be returned.

Concolusion

We can use strproperty of Column Series to process column like string.

Pandas
Column
String
Str
Recommended from ReadMedium