Pandas: Adding Rows

import pandas as pd

persons = pd.DataFrame({
    'firstname': ['Joerg',                  'Johanna',           'Caro',              'Philipp'          ],
    'lastname':  ['Faschingbauer',          'Faschingbauer',     'Faschingbauer',     'Lichtenberger'    ],
    'email':     ['jf@faschingbauer.co.at', 'johanna@email.com', 'caro@email.com',    'philipp@email.com'],
    'age':       [56,                       27,                  25,                  37                 ],
})

Add Rows: Assign To New Row Number

  • Row numbers start at 0

  • Row number behind existing ones: length of DataFrame

  • Assigning to it extends DataFrame

persons2 = persons.copy()
persons2.loc[len(persons2)] = ('Isolde', 'Haubentaucher', '666608091972', 50)
persons2
firstname lastname email age
0 Joerg Faschingbauer jf@faschingbauer.co.at 56
1 Johanna Faschingbauer johanna@email.com 27
2 Caro Faschingbauer caro@email.com 25
3 Philipp Lichtenberger philipp@email.com 37
4 Isolde Haubentaucher 666608091972 50

Add Rows: append() (Deprecated)

persons2 = persons.copy()
try:
    persons2.append({'firstname': 'Isolde', 'lastname': 'Haubentaucher', 'email': 'isi@email.com', 'age': 50}, ignore_index=True)
except FutureWarning:
    pass

Add Rows: Concatenating Frames

  • This appears to be the most recommended approach

new_persons = pd.DataFrame({'firstname': ['Isolde', 'Saskia'],
                          'lastname': ['Haubentaucher', 'Haubentaucher'],
                          'svnr': ['666608091972', '456710042003'],
                          'age': [50, 19]
                         })
pd.concat([persons, new_persons])
firstname lastname email age svnr
0 Joerg Faschingbauer jf@faschingbauer.co.at 56 NaN
1 Johanna Faschingbauer johanna@email.com 27 NaN
2 Caro Faschingbauer caro@email.com 25 NaN
3 Philipp Lichtenberger philipp@email.com 37 NaN
0 Isolde Haubentaucher NaN 50 666608091972
1 Saskia Haubentaucher NaN 19 456710042003