Create a new column in Pandas dataframe
Pandas
Import modules
import pandas as pd
Create some dummy data
raw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
'age': [20, 19, 22, 21],
'favorite_color': ['blue', 'blue', 'yellow', "green"],
'test_one': [88, 92, 95, 70],
'test_two': [78, 100, 90, 85]}
df = pd.DataFrame(raw_data)
df
age | favorite_color | name | test_one | test_two | |
---|---|---|---|---|---|
0 | 20 | blue | Willard Morris | 88 | 78 |
1 | 19 | blue | Al Jennings | 92 | 100 |
2 | 22 | yellow | Omar Mullins | 95 | 90 |
3 | 21 | green | Spencer McDaniel | 70 | 85 |
df['test_average'] = (df.test_one + df.test_two)/2
df.head()
age | favorite_color | name | test_one | test_two | test_average | |
---|---|---|---|---|---|---|
0 | 20 | blue | Willard Morris | 88 | 78 | 83.0 |
1 | 19 | blue | Al Jennings | 92 | 100 | 96.0 |
2 | 22 | yellow | Omar Mullins | 95 | 90 | 92.5 |
3 | 21 | green | Spencer McDaniel | 70 | 85 | 77.5 |