Concatenating and appending DataFrames
The Pandas DataFrame allows operations that are similar to the inner and outer joins of database tables. We can append and concatenate rows as well. To practice appending and concatenating of rows, we will reuse the DataFrame from the previous section. Let's select the first three rows:
print("df :3\n", df[:3])
Check that these are indeed the first three rows:
df :3 Food Number Price Weather 0 soup 8 3.745401 cold 1 soup 5 9.507143 hot 2 icecream 4 7.319939 cold
The concat()
function concatenates DataFrames. For example, we can concatenate a DataFrame that consists of three rows to the rest of the rows, in order to recreate the original DataFrame:
print("Concat Back together\n", pd.concat([df[:3], df[3:]]))
The concatenation output appears as follows:
Concat Back together Food Number Price Weather 0 soup 8 3.745401 cold 1 ...