def convert_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5.0 / 9.0
return celsius
f_deg = 85
c_deg = convert_to_celsius(f_deg)
print(f_deg, "Fahrenheit is", c_deg, "Celsius")85 Fahrenheit is 29.444444444444443 Celsius
def keyword.or
applyimport pandas as pd
columns = ["name", "age", "city", "avg_temp"]
data = [
["Alice", 24, "Toronto", 72],
["Bob", 34, "New York", 74],
["Charlie", 45, "Los Angeles", 88],
]
df = pd.DataFrame(data, columns=columns)
df["age"] = df["age"].apply(lambda x: x + 1)
df| name | age | city | avg_temp | |
|---|---|---|---|---|
| 0 | Alice | 25 | Toronto | 72 |
| 1 | Bob | 35 | New York | 74 |
| 2 | Charlie | 46 | Los Angeles | 88 |
apply, cont.import pandas as pd
columns = ["name", "age", "city", "avg_temp"]
data = [
["Alice", 24, "Toronto", 72],
["Bob", 34, "New York", 74],
["Charlie", 45, "Los Angeles", 88],
]
df = pd.DataFrame(data, columns=columns)
def convert_to_celsius(f):
celsius = (f - 32) * 5.0 / 9.0
return round(celsius, 2)
df["avg_temp"] = df["avg_temp"].apply(convert_to_celsius)
df| name | age | city | avg_temp | |
|---|---|---|---|---|
| 0 | Alice | 24 | Toronto | 22.22 |
| 1 | Bob | 34 | New York | 23.33 |
| 2 | Charlie | 45 | Los Angeles | 31.11 |
.py file NOT .ipynbfrom {filename} import {function}some_name.pyColab Notebooks folder in Google Drivesys.path.append('/content/drive/MyDrive/Colab Notebooks')from some_name import function_nameThen in Colab…