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

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-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

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.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.