Skip to main content
St Louis

Back to all posts

How to Formatting Columns In Pandas?

Published on
3 min read
How to Formatting Columns In Pandas? image

Best Data Analysis Tools to Buy in October 2025

1 The Parent's Survival Guide to PANDAS/PANS: A Handbook to Manage Neuroimmune Disorders in Your Child Without Losing Your Mind

The Parent's Survival Guide to PANDAS/PANS: A Handbook to Manage Neuroimmune Disorders in Your Child Without Losing Your Mind

BUY & SAVE
$19.99
The Parent's Survival Guide to PANDAS/PANS: A Handbook to Manage Neuroimmune Disorders in Your Child Without Losing Your Mind
2 The College Panda's SAT Math: Advanced Guide and Workbook

The College Panda's SAT Math: Advanced Guide and Workbook

BUY & SAVE
$32.49
The College Panda's SAT Math: Advanced Guide and Workbook
3 The College Panda's SAT Writing: Advanced Guide and Workbook

The College Panda's SAT Writing: Advanced Guide and Workbook

BUY & SAVE
$29.95
The College Panda's SAT Writing: Advanced Guide and Workbook
4 Paper Panda's Guide to Papercutting

Paper Panda's Guide to Papercutting

BUY & SAVE
$26.00
Paper Panda's Guide to Papercutting
5 The Comprehensive Physicians' Guide to the Management of PANS and PANDAS: An Evidence-Based Approach to Diagnosis, Testing, and Effective Treatment

The Comprehensive Physicians' Guide to the Management of PANS and PANDAS: An Evidence-Based Approach to Diagnosis, Testing, and Effective Treatment

BUY & SAVE
$51.00 $83.00
Save 39%
The Comprehensive Physicians' Guide to the Management of PANS and PANDAS: An Evidence-Based Approach to Diagnosis, Testing, and Effective Treatment
6 Mastering pandas: A complete guide to pandas, from installation to advanced data analysis techniques, 2nd Edition

Mastering pandas: A complete guide to pandas, from installation to advanced data analysis techniques, 2nd Edition

BUY & SAVE
$42.91 $48.99
Save 12%
Mastering pandas: A complete guide to pandas, from installation to advanced data analysis techniques, 2nd Edition
+
ONE MORE?

To format columns in pandas, you can use the applymap function to apply a formatting function to each element in the column. First, create a formatting function that defines how you want the values in the column to be displayed. Then, use the applymap function on the specific column and pass the formatting function as an argument. This will apply the formatting function to every element in the column and display the formatted values. You can also use the apply function to apply the formatting function element-wise to each element in the column. Additionally, you can use the Astype function to change the data type of the column and customize the display format of the values.

How to format columns in pandas using .str.get() method?

To format columns in pandas using the .str.get() method, you can follow these steps:

  1. Import the pandas library:

import pandas as pd

  1. Create a DataFrame with a column containing strings:

data = {'name': ['John Doe', 'Jane Smith', 'Mark Johnson']} df = pd.DataFrame(data)

  1. Use the .str.get() method to extract characters from a specific position in the strings:

df['first_initial'] = df['name'].str.get(0) df['last_name'] = df['name'].str.get(-1)

In the example above, df['name'].str.get(0) extracts the first character from each string in the 'name' column and assigns it to a new column 'first_initial'. Similarly, df['name'].str.get(-1) extracts the last character from each string in the 'name' column and assigns it to a new column 'last_name'.

  1. Print the updated DataFrame:

print(df)

This will display the DataFrame with the new columns containing the extracted characters from the strings in the 'name' column.

How to format columns in pandas using .str.extract() method with regex?

To format columns in pandas using the .str.extract() method with regex, you can follow these steps:

  1. Import the pandas library:

import pandas as pd

  1. Create a sample DataFrame:

data = {'text': ['a1b', 'c2d', 'e3f']} df = pd.DataFrame(data)

  1. Use the .str.extract() method with regex to extract specific patterns from the 'text' column:

df['letters'] = df['text'].str.extract(r'([a-zA-Z]+)') df['numbers'] = df['text'].str.extract(r'(\d+)')

In this example, we used regex patterns to extract letters and numbers from the 'text' column and created two new columns 'letters' and 'numbers' to store the extracted values.

  1. Print the formatted DataFrame:

print(df)

Output:

text letters numbers 0 a1b a 1 1 c2d c 2 2 e3f e 3

By using the .str.extract() method with regex, you can easily extract and format data from columns in a pandas DataFrame.

How to format columns in pandas using .str.isalpha() method?

To format columns in pandas using the .str.isalpha() method, you can use the following code:

import pandas as pd

Create a sample dataframe

data = {'Name': ['John', 'Emily', '123', 'Alice'], 'Age': [25, 30, 22, 28]}

df = pd.DataFrame(data)

Format the 'Name' column to check if each value is alphabet characters only

df['Name'] = df['Name'].astype(str).str.isalpha()

print(df)

This code will output the following dataframe:

Name  Age

0 True 25 1 True 30 2 False 22 3 True 28

In this example, we used the .str.isalpha() method to check if each value in the 'Name' column contains only alphabet characters. The output is a boolean value indicating whether the condition is satisfied or not.