5 Best Ways to Convert Celsius Data Columns to Fahrenheit in Python Pandas

Rate this post

πŸ’‘ Problem Formulation: Data scientists often work with temperature data in different units and may need to convert between Celsius and Fahrenheit. This article tackles the problem by focusing on a specific challenge: converting a column of temperature data from Celsius to Fahrenheit within a Pandas DataFrame. The input is a Pandas DataFrame with at least one column containing temperature values in Celsius. The desired output is the same DataFrame, but with the temperature values converted to Fahrenheit.

Method 1: Apply a Conversion Function

In the first method, we define a conversion function that applies the Celsius to Fahrenheit formula to each element in the DataFrame column. This function is then applied to the DataFrame using the apply() method, which allows for element-wise transformations.

Here’s an example:

import pandas as pd

def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

df = pd.DataFrame({'Celsius': [0, 25, 30]})
df['Fahrenheit'] = df['Celsius'].apply(celsius_to_fahrenheit)

print(df)

Output:

   Celsius  Fahrenheit
0        0        32.0
1       25        77.0
2       30        86.0

This code snippet defines a straightforward conversion function and leverages pandas’ apply() method to perform the conversion on each value in the ‘Celsius’ column. The resulting Fahrenheit values are stored in a new ‘Fahrenheit’ column.

Method 2: Using a Lambda Function

Lambda functions provide a concise way to write small anonymous functions in Python. By combining lambda functions with the DataFrame’s apply() method, one can inline the conversion logic without defining a separate function.

Here’s an example:

import pandas as pd

df = pd.DataFrame({'Celsius': [0, 25, 30]})
df['Fahrenheit'] = df['Celsius'].apply(lambda c: (c * 9/5) + 32)

print(df)

Output:

   Celsius  Fahrenheit
0        0        32.0
1       25        77.0
2       30        86.0

In this example, a lambda function is used directly within the apply() method to achieve the same result as Method 1. It’s a more compact solution that reduces the need for writing and calling a separate conversion function.

Method 3: Vectorized Operations with Series

Pandas Series objects support vectorized operations, which allow for processing of data without the need for explicit iteration. This method leverages the power of vectorized operations for a more efficient conversion process directly on the Pandas Series.

Here’s an example:

import pandas as pd

df = pd.DataFrame({'Celsius': [0, 25, 30]})
df['Fahrenheit'] = (df['Celsius'] * 9/5) + 32

print(df)

Output:

   Celsius  Fahrenheit
0        0        32.0
1       25        77.0
2       30        86.0

This code directly applies the Celsius to Fahrenheit conversion formula in a vectorized manner to the ‘Celsius’ column of the DataFrame, resulting in faster execution compared to element-wise application using apply().

Method 4: Using DataFrame’s assign() Method

The assign() method in Pandas can be used to create new columns or to overwrite existing ones, using keyword arguments. This method allows for a clean and readable way to perform operations and assign the results to DataFrame columns.

Here’s an example:

import pandas as pd

df = pd.DataFrame({'Celsius': [0, 25, 30]})
df = df.assign(Fahrenheit=(df['Celsius'] * 9/5) + 32)

print(df)

Output:

   Celsius  Fahrenheit
0        0        32.0
1       25        77.0
2       30        86.0

This snippet makes use of the assign() method to directly perform the Celsius to Fahrenheit conversion and add the result as a new column. It’s both elegant and functional, keeping the DataFrame’s method chain unbroken.

Bonus One-Liner Method 5: In-Place Series Modification

For those looking for the ultimate in code brevity, the temperature conversion can be done using an in-place modification of a Series. This method alters the original ‘Celsius’ column without the need for creating an additional column.

Here’s an example:

import pandas as pd

df = pd.DataFrame({'Celsius': [0, 25, 30]})
df['Celsius'] *= 9/5
df['Celsius'] += 32

print(df)

Output:

   Celsius
0     32.0
1     77.0
2     86.0

This two-line code snippet directly modifies the ‘Celsius’ column by doing the conversion in-place. This method reduces memory usage and keeps the conversion concise.

Summary/Discussion

  • Method 1: Using a conversion function. Strengths: Readable and reusable for other conversions. Weaknesses: Slightly more verbose than the other methods.
  • Method 2: Using a lambda function. Strengths: Compact code without the need for separate function definition. Weaknesses: Less readable for complex functions.
  • Method 3: Vectorized Series operations. Strengths: Efficient computation. Weaknesses: No explicit function, which might make it harder to reuse.
  • Method 4: Using DataFrame’s assign() method. Strengths: Clean syntax for method chaining. Weaknesses: May be less intuitive for newcomers to Pandas.
  • Bonus Method 5: In-Place Series modification. Strengths: Most memory efficient and terse. Weaknesses: Alters the original data, which might not always be desirable.