How to concatenate 2 dataframes?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to concatenate 2 dataframes?

How to concatenate 2 dataframes?

This recipe helps you concatenate 2 dataframes

0

Recipe Objective

While operating with pandas, we might be interested to concat 2 or more dataframes as per our requirement (either at row or columns). Concat has special feature to handle multiple dataframes in one go.

So this recipe is a short example on how to concatenate 2 or more dataframes. Let's get started.

Step 1 - Import the library

import pandas as pd import seaborn as sb

Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays. Seaborn is just for importing dataset for now.

Step 2 - Setup the Data

df = sb.load_dataset('tips') df.to_csv('tips.csv') df_a=df.iloc[:,0:2] df_b=df.iloc[:,2:4] df_c=df.iloc[:,4:]

Here we have simply imported tips dataset from seaborn library. Now we have broken down the dataset into 3 dataframes: df_a, df_b and df_c.

Step 3 - Concating dataframes created

df_merged = pd.concat([df_a, df_b,df_c], axis=1)

Here, we are merging 3 dataframes on columns. We can do various other operation while concating.

Step 4 - Printing the results

print(df) print(df_a) print(df_b) print(df_c) print(df_merged)

Simply use print function to print all the dataframes created

Step 4 - Let's look at our dataset now

Once we run the above code snippet, we will see:

Scroll down the ipython file to visualize the final output.

Relevant Projects

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.