pandas.concat() function in Python
Last Updated :
24 Jun, 2025
pandas.concat() function concatenate two or more pandas objects like DataFrames or Series along a particular axis. It is especially useful when combining datasets either vertically (row-wise) or horizontally (column-wise). Example:
Python
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', 'B1']})
df2 = pd.DataFrame({'A': ['A2', 'A3'], 'B': ['B2', 'B3']})
res = pd.concat([df1, df2])
print(res)
Output A B
0 A0 B0
1 A1 B1
0 A2 B2
1 A3 B3
Explanation: pd.concat([df1, df2]) stacks DataFrames row-wise and keeps their original indices, causing duplicates.
Syntax
pandas.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, ...)
Parameters:
Parameter | Description |
---|
objs | A list or tuple of pandas objects (DataFrames or Series) to concatenate |
---|
axis | The axis along which to concatenate: 0 for rows (default), 1 for columns |
---|
join | How to handle indexes: 'outer' (default) or 'inner' |
---|
ignore_index | If True, the index will be reset in the result |
---|
keys | Create a hierarchical index using passed keys |
---|
Returns: A new pandas object (typically a DataFrame) that is the result of concatenation.
Examples
Example 1: In this, we are concatenating two DataFrames side by side (column-wise) using axis=1.
Python
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1']})
df2 = pd.DataFrame({'B': ['B0', 'B1']})
res = pd.concat([df1, df2], axis=1)
print(res)
Output A B
0 A0 B0
1 A1 B1
Explanation: pd.concat([df1, df2], axis=1) joins DataFrames column-wise, aligning rows by index. It places the columns of df2 to the right of df1, forming a wider DataFrame.
Example 2: In this, we are concatenating two DataFrames vertically (row-wise) and resetting the index using ignore_index=True.
Python
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1']})
df2 = pd.DataFrame({'B': ['B0', 'B1']})
res = pd.concat([df1, df2], ignore_index=True)
print(res)
Output A B
0 A0 NaN
1 A1 NaN
2 NaN B0
3 NaN B1
Explanation: pd.concat([df1, df2], ignore_index=True) stacks the DataFrames row-wise and resets the index in the result. Since the columns don’t match, pandas fills missing values with NaN.
Example 3: In this, we are concatenating two DataFrames vertically and labeling each with a group name using keys.
Python
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1']})
df2 = pd.DataFrame({'B': ['B0', 'B1']})
res = pd.concat([df1, df2], keys=['group1', 'group2'])
print(res)
Output A B
group1 0 A0 NaN
1 A1 NaN
group2 0 NaN B0
1 NaN B1
Explanation: pd.concat([df1, df2], keys=['group1', 'group2']) stacks DataFrames vertically and adds a hierarchical index. The keys label each block, making it easier to identify which DataFrame each row came from.
Example 4: In this, we are concatenating two DataFrames but keeping only the common columns using join='inner' and resetting index.
Python
import pandas as pd
df1 = pd.DataFrame({'A': ['A0'], 'B': ['B0']})
df2 = pd.DataFrame({'B': ['B1'], 'C': ['C1']})
res = pd.concat([df1, df2], join='inner', ignore_index=True)
print(res)
Explanation: pd.concat([df1, df2], join='inner', ignore_index=True) combines DataFrames row-wise, keeping only the common columns ('B' here). ignore_index=True resets the index in the final result.
Similar Reads
Python | Pandas dataframe.median() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.median() function return the median of the values for the requested a
2 min read
Python | Pandas Series.std() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.std() function return sample
2 min read
Apply function to every row in a Pandas DataFrame Applying a function to every row in a Pandas DataFrame means executing custom logic on each row individually. For example, if a DataFrame contains columns 'A', 'B' and 'C', and you want to compute their sum for each row, you can apply a function across all rows to generate a new column. Letâs explor
3 min read
Joining two Pandas DataFrames using merge() The merge() function is designed to merge two DataFrames based on one or more columns with matching values. The basic idea is to identify columns that contain common data between the DataFrames and use them to align rows. Let's understand the process of joining two pandas DataFrames using merge(), e
4 min read
Pandas DataFrame.astype()-Python DataFrame.astype() function in pandas cast a pandas object such as a DataFrame or Series to a specified data type. This is especially useful when you need to ensure that columns have the correct type, such as converting strings to integers or floats to strings. For example:Pythonimport pandas as pd
3 min read
Python | Pandas DataFrame.set_index() Pandas set_index() method is used to set one or more columns of a DataFrame as the index. This is useful when we need to modify or add new indices to our data as it enhances data retrieval, indexing and merging tasks. Setting the index is helpful for organizing the data more efficiently, especially
3 min read
Pandas DataFrame.reset_index() The reset_index() method in Pandas is used to manage and reset the index of a DataFrame. It is useful after performing operations that modify the index such as filtering, grouping or setting a custom index. By default reset_index() reverts to a clean, default integer-based index (0, 1, 2, ...) which
4 min read
Python | Pandas Dataframe.at[ ] Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas at[] is used to return data in a dataframe at the passed location. The passed l
2 min read
Pandas DataFrame iterrows() Method iterrows() method in Pandas is a simple way to iterate over rows of a DataFrame. It returns an iterator that yields each row as a tuple containing the index and the row data (as a Pandas Series). This method is often used in scenarios where row-wise operations or transformations are required. Exampl
4 min read
Python | Pandas Series.iteritems() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.iteritems() function iterates
2 min read