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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.