Changing height feet and inches to an integer in python pandas

When working with data in Python, it is common to encounter situations where we need to convert values from one data type to another. One such scenario is when we have a column in a pandas DataFrame that represents height in feet and inches, and we want to convert it to an integer representing the total height in inches.

Option 1: Using a Custom Function

One way to solve this problem is by defining a custom function that takes the height in feet and inches as input and returns the total height in inches. Here’s an example:

import pandas as pd

def convert_to_inches(height):
feet, inches = height.split("'")
total_inches = int(feet) * 12 + int(inches.strip('"'))
return total_inches

# Example usage
df = pd.DataFrame({'Height': ["5'10""

Rate this post

6 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents