Concatenating multiple DataFrames together
The concat
function enables concatenating two or more DataFrames (or Series) together, both vertically and horizontally. As per usual, when dealing with multiple pandas objects simultaneously, concatenation doesn't happen haphazardly but aligns each object by their index.
In this recipe, we combine DataFrames both horizontally and vertically with the concat
function and then change the parameter values to yield different results.
How to do it…
- Read in the 2016 and 2017 stock datasets, and make their ticker symbol the index:
>>> stocks_2016 = pd.read_csv('data/stocks_2016.csv', ... index_col='Symbol') >>> stocks_2017 = pd.read_csv('data/stocks_2017.csv', ... index_col='Symbol') >>> stocks_2016 Shares Low High Symbol AAPL 80 95 110 TSLA 50 80 130 WMT 40 55 70 ...