How to format string in a Pandas DataFrame Column?

This recipe helps you format string in a Pandas DataFrame Column

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.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

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)

 

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

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

Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!

Download Materials

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python