How to format string in a Pandas DataFrame Column?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to format string in a Pandas DataFrame Column?

How to format string in a Pandas DataFrame Column?

This recipe helps you format string in a Pandas DataFrame Column

0

Recipe Objective

Have you ever feel a need to change the format of the string like while working with names then we need to change the lower case letters to upper case or vice versa. So for this there are predefined functions are available in python.

This python source code does the following :
1. Creates a pandas series
2. Converts strings into lower and upper format
3. performs splits and capitalization

So this is the recipe on how we can format string in a Pandas DataFrame Column.

Step 1 - Import the library

import pandas as pd

We have imported one library that is pandas which is only need for this.

Step 2 - Setting up the Data

We have created a list of names as a data on which we will be doing all the formatting. first_names = pd.Series(['Sheldon Cooper', 'Leonard Hofstadter', 'Howard Wolowitz', 'Raj Koothrappali']) print(first_names)

Step 3 - Performing different types of Formating

So we will be doing different types of formating

  • Printing all the letters in lower case of data in first_names
  • print(first_names.str.lower())
  • Printing all the letters in upper case of data in first_names
  • print(first_names.str.upper())
  • Printing all the letters such that first letter of every word is in upper case and rest in lower case
  • print(first_names.str.title())
  • Printing all the letters after spliting the the words into two parts i.e name and title seperately
  • print(first_names.str.split(" "))
  • Printing all the letters such that only first letter of each name is in upper case and rest in lower case
  • print(first_names.str.capitalize())
So the output comes as:

0        Sheldon Cooper
1    Leonard Hofstadter
2       Howard Wolowitz
3      Raj Koothrappali
dtype: object

0        sheldon cooper
1    leonard hofstadter
2       howard wolowitz
3      raj koothrappali
dtype: object

0        SHELDON COOPER
1    LEONARD HOFSTADTER
2       HOWARD WOLOWITZ
3      RAJ KOOTHRAPPALI
dtype: object

0        Sheldon Cooper
1    Leonard Hofstadter
2       Howard Wolowitz
3      Raj Koothrappali
dtype: object

0        [Sheldon, Cooper]
1    [Leonard, Hofstadter]
2       [Howard, Wolowitz]
3      [Raj, Koothrappali]
dtype: object

0        Sheldon cooper
1    Leonard hofstadter
2       Howard wolowitz
3      Raj koothrappali
dtype: object

Relevant Projects

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.