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

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.

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.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.