Home      |       Contents       |       About

Prev: Modifying Columns in DataFrame       |      Next: Plotting a Series/DataFrame object

Draft version

Modifying Rows in DataFrame

Below you can see a simple example of how to use the pd.concat() method to add new rows to a DataFrame

  • _Please come back later to find information about other techniques on how to add rows to a DataFrame object

1. Add rows with 'concat'

  • pandas offer a powerful 'concat' method to concatenate DataFrames or Series objects
  • See more about concat() here
In [36]:
import numpy as np 
import pandas as pd

data1 = {'country': ['Italy','Spain','Greece','France','Portugal'],
        'popu': [61, 46, 11, 65, 10]
        }
data2 = {'country': ['Brazil','Argentina'],
        'popu': [207, 44]
        }

df1 = pd.DataFrame(data1, index=['ITA', 'ESP', 'GRC', 'FRA', 'PRT'])
print(df1,'\n')

df2 = pd.DataFrame(data2, index=['BRA', 'ARG'])
print(df2)

total = pd.concat([df2, df1])
total
      country  popu
ITA     Italy    61
ESP     Spain    46
GRC    Greece    11
FRA    France    65
PRT  Portugal    10 

       country  popu
BRA     Brazil   207
ARG  Argentina    44
Out[36]:
country popu
BRA Brazil 207
ARG Argentina 44
ITA Italy 61
ESP Spain 46
GRC Greece 11
FRA France 65
PRT Portugal 10

. Free learning material
. See full copyright and disclaimer notice