pandas DataFrame append: How to append or concatenate DataFrames and reindex

pandas DataFrame can be appended or concatenated by append method like Python list.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
]

b = [
    ('California', 40),
    ('Florida', 21),
]

df1 = pandas.DataFrame(a, columns=['State', 'Population'])
df2 = pandas.DataFrame(b, columns=['State', 'Population'])

df3 = df1.append(df2)

print(df1)
#       State  Population
# 0     Texas          29
# 1  New York          19

print(df2)
#         State  Population
# 0  California          40
# 1     Florida          21

print(df3)
#         State  Population
# 0       Texas          29
# 1    New York          19
# 0  California          40
# 1     Florida          21

df3 is simply df1 + df2 so the indexes of df3 are 0, 1, 0, 1, which no longer express the index of DataFrame. If you don't want those indexes and reindex, set ignore_index option True in the method.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
]

b = [
    ('California', 40),
    ('Florida', 21),
]

df1 = pandas.DataFrame(a, columns=['State', 'Population'])
df2 = pandas.DataFrame(b, columns=['State', 'Population'])

df3 = df1.append(df2, ignore_index=True)

print(df3)
#         State  Population
# 0       Texas          29
# 1    New York          19
# 2  California          40
# 3     Florida          21

pandas DataFrame

pandas tutorial