This tutorial has described various techniques to append rows to existing DataFrame with examples.
In [1]:
import pandas as pd
d = {'one' : [10,20,30],'two' : ['a','b','c']}
df = pd.DataFrame(d)
df
Out[1]:
one two
0 10 a
1 20 b
2 30 c
Pandas’ .loc method used to append the data to DataFrame.
Appending a row by given list –
In [2]: df.loc[3] = [40,'d'] # add the list value at specified index 3 Out[2]: one two 0 10 a 1 20 b 2 30 c 3 40 d
Appending a row by given dictionary –
In [3]: df.loc[4] = {'one':50,'two':'e'} # add the dict value at specified index 4
Out[3]:
one two
0 10 a
1 20 b
2 30 c
3 40 d
4 50 e
Appending a row by single column value –
You can specify the value of a specific column while appending the new row. The NaN value is assigned to the non-specified column automatically.
In [4]: df.loc[5,'one'] = 60 # add value 60 to the column 'one' at specified index 5
Out[4]:
one two
0 10.0 a
1 20.0 b
2 30.0 c
3 40.0 d
4 50.0 e
5 60.0 NaN
Note: If you use an existing index in df.loc[], this will overwrite the value in that specified row.
In [5]: df.loc[2] = [200,'z'] # This will overwrite the column values at index row 2.
Out[5]:
one two
0 10.0 a
1 20.0 b
2 200.0 z
3 40.0 d
4 50.0 e
5 60.0 NaN
In [6]: df.loc[1,'two'] = 'Y' #This will overwrite column value 'two' at index row 1
Out[6]:
one two
0 10.0 a
1 20.0 Y
2 200.0 z
3 40.0 d
4 50.0 e
5 60.0 NaN
Appending DataFrame to another DataFrame –
Pandas’ append() function used to append one DataFrame to another DataFrame. The append function doesn’t change any of the original DataFrames, it will return a new DataFrame by appending two DataFrame.
# Let's define a new DataFrame, which will be inserted to DataFrame df
In [7]: temp_df = pd.DataFrame([[4.0,5]],columns=['one','two'])
In [8]: temp_df
Out[8]:
one two
0 4.0 5
In [9]: df2 = df.append(temp_df) # temp_df DataFrame is appending to DataFrame df
In [10]: df2
Out[10]:
one two
0 10.0 a
1 20.0 Y
2 200.0 z
3 40.0 d
4 50.0 e
5 60.0 NaN
0 4.0 5
In [11]: df2 = df.append(temp_df,ignore_index=True) # avoid duplicate index
In [12]: df2
Out[12]:
one two
0 10.0 a
1 20.0 Y
2 200.0 z
3 40.0 d
4 50.0 e
5 60.0 NaN
6 4.0 5
Note – While appending DataFrames, it is not necessary to have the same set of columns to both DataFrame.
# Let's define DataFrame df_A
In [13]: df_A = pd.DataFrame({'A' : [10,20],'B' : ['a','b']})
In [14]: df_A
Out[14]:
A B
0 10 a
1 20 b
# Let's define DataFrame df_B
In [15]: df_B = pd.DataFrame({'B':['x','y'],'C':[12,14]})
In [16]: df_B
Out[16]:
B C
0 x 12
1 y 14
# Append DataFrame df_B to df_A
In [17]: df3 = df_A.append(df_B)
In [18]: df3
Out[18]:
A B C
0 10.0 a NaN
1 20.0 b NaN
0 NaN x 12.0
1 NaN y 14.0
. . .