Articles → Pandas → Concat Function In Pandas
Concat Function In Pandas
Purpose
Example
import numpy as np
import pandas as pd
# Create a dictionary
dict1 = {'company': [
'Google',
'Microsoft',
'Apple',
'Google',
'Microsoft',
'Apple',
], 'year': [
2017,
2017,
2017,
2018,
2018,
2018,
], 'Profit': [
20000,
40000,
60000,
80000,
70000,
50000,
]}
dict2 = {'company': [
'Disney',
'Walmart',
'AT&T',
'Disney',
'Walmart',
'AT&T',
], 'year': [
2017,
2017,
2017,
2018,
2018,
2018,
], 'Profit': [
10000,
20000,
30000,
40000,
50000,
60000,
]}
dict3 = {'company': [
'Verizon',
'Volkswagen',
'Nestl\xc3\xa9',
'Verizon',
'Volkswagen',
'Nestl\xc3\xa9',
], 'year': [
2017,
2017,
2017,
2018,
2018,
2018,
], 'Profit': [
30000,
20000,
30000,
40000,
60000,
60000,
]}
# Create a dataframe
df1 = pd.DataFrame(dict1, index=[
0,
1,
2,
3,
4,
5,
])
df2 = pd.DataFrame(dict2, index=[
6,
7,
8,
9,
10,
11,
])
df3 = pd.DataFrame(dict3, index=[
12,
13,
14,
15,
16,
17,
])
# Concat
pd.concat([df1, df2, df3])
Click to Enlarge
Concat Row-Wise
import numpy as np
import pandas as pd
# Create a dictionary
dict1 = {'company': [
'Google',
'Microsoft',
'Apple',
'Google',
'Microsoft',
'Apple',
], 'year': [
2017,
2017,
2017,
2018,
2018,
2018,
], 'Profit': [
20000,
40000,
60000,
80000,
70000,
50000,
]}
dict2 = {'company': [
'Disney',
'Walmart',
'AT&T',
'Disney',
'Walmart',
'AT&T',
], 'year': [
2017,
2017,
2017,
2018,
2018,
2018,
], 'Profit': [
10000,
20000,
30000,
40000,
50000,
60000,
]}
dict3 = {'company': [
'Verizon',
'Volkswagen',
'Nestl\xc3\xa9',
'Verizon',
'Volkswagen',
'Nestl\xc3\xa9',
], 'year': [
2017,
2017,
2017,
2018,
2018,
2018,
], 'Profit': [
30000,
20000,
30000,
40000,
60000,
60000,
]}
# Create a dataframe
df1 = pd.DataFrame(dict1, index=[
0,
1,
2,
3,
4,
5,
])
df2 = pd.DataFrame(dict2, index=[
6,
7,
8,
9,
10,
11,
])
df3 = pd.DataFrame(dict3, index=[
12,
13,
14,
15,
16,
17,
])
# Concat
pd.concat([df1, df2, df3], axis=1)
Click to Enlarge