Python ProgrammingPython Programming

Adding new column to existing DataFrame in Pandas

Add Column to DataFrame:

import pandas as pd

employees = pd.DataFrame({
    'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'],
    'Name': ['John', 'Doe', 'William', 'Spark', 'Mark'],
    'Occupation': ['Chemist', 'Statistician', 'Statistician',
                   'Statistician', 'Programmer'],
    'Date Of Join': ['2018-01-25', '2018-01-26', '2018-01-26', '2018-02-26',
                     '2018-03-16'],
    'Age': [23, 24, 34, 29, 40]})

employees['City'] = ['London', 'Tokyo', 'Sydney', 'London', 'Toronto']

print(employees)


C:\python\pandas examples>python example9.py
   Age Date Of Join EmpCode     Name    Occupation     City
0   23   2018-01-25  Emp001     John       Chemist   London
1   24   2018-01-26  Emp002      Doe  Statistician    Tokyo
2   34   2018-01-26  Emp003  William  Statistician   Sydney
3   29   2018-02-26  Emp004    Spark  Statistician   London
4   40   2018-03-16  Emp005     Mark    Programmer  Toronto
 
C:\python\pandas examples>