``````

In [1]:

import pandas as pd

``````
``````

In [2]:

df = pd.DataFrame({'A': ['A1', 'A2', 'A3'],
'B': ['B1', 'B2', 'B3'],
'C': ['C1', 'C2', 'C3']},
index=['ONE', 'TWO', 'THREE'])
print(df)

``````
``````

A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3

``````
``````

In [3]:

df.loc['ONE'] = 0
print(df)

``````
``````

A   B   C
ONE     0   0   0
TWO    A2  B2  C2
THREE  A3  B3  C3

``````
``````

In [4]:

df.loc['FOUR'] = 0
df.loc['FIVE'] = ['A5', 'B5', 'C5']
print(df)

``````
``````

A   B   C
ONE     0   0   0
TWO    A2  B2  C2
THREE  A3  B3  C3
FOUR    0   0   0
FIVE   A5  B5  C5

``````
``````

In [5]:

# df.loc['SIX'] = ['A6', 'B6']
# ValueError: cannot set a row with mismatched columns

``````
``````

In [6]:

s = pd.Series(['B6', 'C6', 'D6'], index=['B', 'C', 'D'], name='SIX')
print(s)

``````
``````

B    B6
C    C6
D    D6
Name: SIX, dtype: object

``````
``````

In [7]:

df.loc['XXX'] = df.loc['TWO'] + df.loc['THREE']
df.loc['YYY'] = s
df.loc['ZZZ'] = s.values
print(df)

``````
``````

A     B     C
ONE       0     0     0
TWO      A2    B2    C2
THREE    A3    B3    C3
FOUR      0     0     0
FIVE     A5    B5    C5
XXX    A2A3  B2B3  C2C3
YYY     NaN    B6    C6
ZZZ      B6    C6    D6

``````
``````

In [8]:

df = pd.DataFrame({'A': ['A1', 'A2', 'A3'],
'B': ['B1', 'B2', 'B3'],
'C': ['C1', 'C2', 'C3']},
index=['ONE', 'TWO', 'THREE'])

``````
``````

In [9]:

# print(df.append(0))
# TypeError: cannot concatenate object of type '<class 'int'>'; only Series and DataFrame objs are valid

``````
``````

In [10]:

print(df.append([0, 1, 2]))

``````
``````

A    B    C    0
ONE     A1   B1   C1  NaN
TWO     A2   B2   C2  NaN
THREE   A3   B3   C3  NaN
0      NaN  NaN  NaN  0.0
1      NaN  NaN  NaN  1.0
2      NaN  NaN  NaN  2.0

``````
``````

In [11]:

print(df.append({'A': 0, 'B': 1, 'C': 2}, ignore_index=True))

``````
``````

A   B   C
0  A1  B1  C1
1  A2  B2  C2
2  A3  B3  C3
3   0   1   2

``````
``````

In [12]:

s = pd.Series(['A4', 'B4', 'C4'], index=df.columns, name='FOUR')

``````
``````

In [13]:

print(df.append(s))

``````
``````

A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
FOUR   A4  B4  C4

``````
``````

In [14]:

s_mismatch = pd.Series(['B5', 'C5', 'D5'], index=['B', 'C', 'D'], name='FIVE')

``````
``````

In [15]:

print(df.append(s_mismatch))

``````
``````

A   B   C    D
ONE     A1  B1  C1  NaN
TWO     A2  B2  C2  NaN
THREE   A3  B3  C3  NaN
FIVE   NaN  B5  C5   D5

``````
``````

In [16]:

print(df)

``````
``````

A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3

``````
``````

In [17]:

s_no_name = pd.Series(['B4', 'C4', 'D4'], index=['B', 'C', 'D'])

``````
``````

In [18]:

# print(df.append(s_no_name))
# TypeError: Can only append a Series if ignore_index=True or if the Series has a name

``````
``````

In [19]:

print(df.append(s_no_name, ignore_index=True))

``````
``````

A   B   C    D
0   A1  B1  C1  NaN
1   A2  B2  C2  NaN
2   A3  B3  C3  NaN
3  NaN  B4  C4   D4

``````
``````

In [20]:

print(df.append([s, s_mismatch]))

``````
``````

A   B   C    D
ONE     A1  B1  C1  NaN
TWO     A2  B2  C2  NaN
THREE   A3  B3  C3  NaN
FOUR    A4  B4  C4  NaN
FIVE   NaN  B5  C5   D5

``````
``````

In [21]:

df2 = pd.DataFrame([['B6', 'C6', 'D6'], ['B7', 'C7', 'D7']],
index=['SIX', 'SEVEN'], columns=['B', 'C', 'D'])
print(df2)

``````
``````

B   C   D
SIX    B6  C6  D6
SEVEN  B7  C7  D7

``````
``````

In [22]:

print(df.append(df2))

``````
``````

A   B   C    D
ONE     A1  B1  C1  NaN
TWO     A2  B2  C2  NaN
THREE   A3  B3  C3  NaN
SIX    NaN  B6  C6   D6
SEVEN  NaN  B7  C7   D7

``````
``````

In [23]:

# print(df.append([s, df2]))
# ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 3

``````
``````

In [24]:

print(df.append(s).append(df2))

``````
``````

A   B   C    D
ONE     A1  B1  C1  NaN
TWO     A2  B2  C2  NaN
THREE   A3  B3  C3  NaN
FOUR    A4  B4  C4  NaN
SIX    NaN  B6  C6   D6
SEVEN  NaN  B7  C7   D7

``````
``````

In [25]:

print(pd.concat([df, df2]))

``````
``````

A   B   C    D
ONE     A1  B1  C1  NaN
TWO     A2  B2  C2  NaN
THREE   A3  B3  C3  NaN
SIX    NaN  B6  C6   D6
SEVEN  NaN  B7  C7   D7

``````
``````

In [26]:

print(pd.concat([df, df2], join='inner'))

``````
``````

B   C
ONE    B1  C1
TWO    B2  C2
THREE  B3  C3
SIX    B6  C6
SEVEN  B7  C7

``````
``````

In [27]:

print(df.append(s))

``````
``````

A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
FOUR   A4  B4  C4

``````
``````

In [28]:

print(pd.concat([df, s]))

``````
``````

A    B    C    0
ONE     A1   B1   C1  NaN
TWO     A2   B2   C2  NaN
THREE   A3   B3   C3  NaN
A      NaN  NaN  NaN   A4
B      NaN  NaN  NaN   B4
C      NaN  NaN  NaN   C4

``````
``````

In [29]:

print(pd.DataFrame(s).T)

``````
``````

A   B   C
FOUR  A4  B4  C4

``````
``````

In [30]:

print(pd.concat([df, pd.DataFrame(s).T]))

``````
``````

A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
FOUR   A4  B4  C4

``````
``````

In [31]:

print(df.T.assign(FOUR=0, FIVE=['A5', 'B5', 'C5']).T)

``````
``````

A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
FOUR    0   0   0
FIVE   A5  B5  C5

``````
``````

In [32]:

# df_insert = df.T.insert(0, 'FOUR', 0).T
# AttributeError: 'NoneType' object has no attribute 'T'

``````
``````

In [33]:

df_T = df.T
df_T.insert(0, 'FOUR', 0)
print(df_T.T)

``````
``````

A   B   C
FOUR    0   0   0
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3

``````