The result for converting celsius to Fahrenheit as,
Id Celsius Fahrenheit 0 1 37.5 99.5 1 2 36.0 96.8 2 3 40.0 104.0 3 4 38.5 101.3 4 5 39.0 102.2
To solve this, we will follow below approaches −
Solution 1
Define a dataframe with ‘Id’ and ‘Celsius’ column values
Apply df.assign function inside write lambda function to convert celsius values by multiplying (9/5)*df[celsius]+32 and assign it to Fahrenheit. It is defined below −
df.assign(Fahrenheit = lambda x: (9/5)*x['Celsius']+32)
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3,4,5], 'Celsius':[37.5,36,40,38.5,39] }) print("DataFrame is\n",df) df = df.assign(Fahrenheit = lambda x: (9/5)*x['Celsius']+32) print(df)
Output
DataFrame is Id Celsius 0 1 37.5 1 2 36.0 2 3 40.0 3 4 38.5 4 5 39.0 Id Celsius Fahrenheit 0 1 37.5 99.5 1 2 36.0 96.8 2 3 40.0 104.0 3 4 38.5 101.3 4 5 39.0 102.2
Solution 2
Define a dataframe with ‘Id’ and ‘Celsius’ column values
Set df.apply function inside writing lambda function to convert celsius values by multiplying (9/5)*df[celsius]+32 and save it as inside df[Fahrenheit]. It is defined below,
df['Fahrenheit'] = df.apply(lambda x: (9/5)*x['Celsius']+32,axis=1)
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3,4,5], 'Celsius':[37.5,36,40,38.5,39] }) print("DataFrame is\n",df) df['Fahrenheit'] = df.apply(lambda x: (9/5)*x['Celsius']+32,axis=1) print(df)
Output
DataFrame is Id Celsius 0 1 37.5 1 2 36.0 2 3 40.0 3 4 38.5 4 5 39.0 Id Celsius Fahrenheit 0 1 37.5 99.5 1 2 36.0 96.8 2 3 40.0 104.0 3 4 38.5 101.3 4 5 39.0 102.2