Combining
Combine multiple DataFrames through concatenation and merging.
We'll cover the following...
Chapter Goals:
- Understand the methods used to combine DataFrame objects
- Write code for combining DataFrames
In the previous chapter, we discussed the append
function for concatenating DataFrame rows. To concatenate multiple DataFrames along either rows or columns, we use the pd.concat
function.
The code below shows example usages of pd.concat
.
Press + to interact
df1 = pd.DataFrame({'c1':[1,2], 'c2':[3,4]},index=['r1','r2'])df2 = pd.DataFrame({'c1':[5,6], 'c2':[7,8]},index=['r1','r2'])df3 = pd.DataFrame({'c1':[5,6], 'c2':[7,8]})concat = pd.concat([df1, df2], axis=1)# Newline to separate print statementsprint('{}\n'.format(concat))concat = pd.concat([df2, df1, df3])print('{}\n'.format(concat))concat = pd.concat([df1, df3], axis=1)print('{}\n'.format(concat))
The pd.concat
function ...