Skip to main content

Pandas Concat: Combining DataFrames

Pandas is a powerful Python library for data manipulation and analysis. One of its most useful features is the concat() function, which allows you to combine multiple DataFrames into a single DataFrame. This can be useful for a variety of tasks, such as:

  • Merging data from different sources
  • Combining data from different time periods
  • Creating a single DataFrame from multiple smaller DataFrames

How to Use Pandas Concat

The concat() function takes a list of DataFrames as its first argument. The DataFrames must have the same number of columns, but the rows can be different. The concat() function will stack the DataFrames vertically, creating a single DataFrame with the combined rows.

The following example shows how to use the concat() function to combine two DataFrames:

import pandas as pd # Create two DataFrames df1 = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'age': [20, 30, 40]}) df2 = pd.DataFrame({'name': ['Dave', 'Eve', 'Frank'], 'age': [50, 60, 70]}) # Combine the DataFrames using concat() df = pd.concat([df1, df2]) # Print the combined DataFrame print(df)



Output: name age 0 Alice 20 1 Bob 30 2 Charlie 40 3 Dave 50 4 Eve 60 5 Frank 70


Additional Options

The concat() function has a number of additional options that you can use to customize the concatenation process. These options include:

  • axis: Specifies the axis along which to concatenate the DataFrames. The default value is 0, which means to concatenate the DataFrames vertically. You can also specify 1 to concatenate the DataFrames horizontally.
  • join: Specifies how to handle duplicate rows when concatenating the DataFrames. The default value is 'outer', which means that all rows from both DataFrames will be included in the combined DataFrame. You can also specify 'inner' to include only the rows that are common to both DataFrames, or 'left' or 'right' to include only the rows from the left or right DataFrame, respectively.
  • ignore_index: Specifies whether to ignore the index of the DataFrames when concatenating them. The default value is False, which means that the index of the combined DataFrame will be the union of the indices of the original DataFrames. You can specify True to ignore the index of the original DataFrames and create a new index for the combined DataFrame.

Conclusion

The concat() function is a powerful tool that can be used to combine multiple DataFrames into a single DataFrame. By understanding how to use the concat() function and its various options, you can easily combine data from different sources, time periods, or smaller DataFrames to create a single, comprehensive DataFrame.

Comments

Archive

Show more

Topics

Show more