Creating New Columns in a DataFrame

  • In pandas, we can create new columns in a DataFrame by performing calculations on existing columns.
    • For example, imagine we wanted to calculate the life expectancy change from one year to the next within each country.
python
1# Import the pandas library
2import pandas as pd
3# Read the data file into a pandas DataFrame
4df = pd.read_csv('data/gapminder.tsv', sep='\t')
5df['lifeExp_change'] = df.groupby('country')['lifeExp'].diff() # Calculate the life expectancy
6print(df.head()) 

Output:

1       country continent  year  lifeExp       pop   gdpPercap  lifeExp_change
20  Afghanistan      Asia  1952   28.801   8425333  779.445314             NaN
31  Afghanistan      Asia  1957   30.332   9240934  820.853030           1.531
42  Afghanistan      Asia  1962   31.997  10267083  853.100710           1.665
53  Afghanistan      Asia  1967   34.020  11537966  836.197138           2.023
64  Afghanistan      Asia  1972   36.088  13079460  739.981106           2.068
  • On line 6 above, we use the diff() function to calculate the difference between consecutive values in the lifeExp column for each country. This function calculates the difference between the current value and the previous value in the column. The first value in each group will be NaN since there is no previous value to calculate the difference from.