You can add a new column to a DataFrame by doing df[new_column_name] = value_for_each_row. Typically, you will create a new column from one or more existing columns in your DataFrame.
Most operators that work on single values in python will work on DataFrames such as +, -, *, /, ==, <, and >.
Using the DataFrame:
import pandas as pd df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]})
| name | age | |
|---|---|---|
| 1 | Jeff | 30 |
| 2 | Esha | 56 |
| 3 | Jia | 8 |
Write a function, add_can_drive(df) which takes in the DataFrame and returns a new DataFrame with a column can_drive which is True if the person is 16 or older and False otherwise.
df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]})
| name | age | |
|---|---|---|
| 0 | Jeff | 30 |
| 1 | Esha | 56 |
| 2 | Jia | 8 |
| age | |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 2 | 0 |