Home > Backend Development > Python Tutorial > How to Combine Pandas DataFrames Side-by-Side?

How to Combine Pandas DataFrames Side-by-Side?

DDD
Release: 2024-10-30 08:18:27
Original
804 people have browsed it

How to Combine Pandas DataFrames Side-by-Side?

Concatenating Dataframes Horizontally in Pandas

When working with data in Python, it's often necessary to combine multiple datasets into a single dataframe. In pandas, concatenation is a powerful operation that allows you to merge dataframes either horizontally or vertically. This article focuses on horizontal concatenation, also known as column-wise concatenation.

Horizontal Concatenation

To horizontally concatenate two dataframes, df_a and df_b, use the concat() function with the axis parameter set to 1:

<code class="python">pd.concat([df_a, df_b], axis=1)</code>
Copy after login

This operation will combine the columns of df_a and df_b into a single dataframe, with the rows aligned vertically. The resulting dataframe will have the same number of rows as the original dataframes and a number of columns equal to the sum of the number of columns in both dataframes.

Example

Consider the following two dataframes:

<code class="python">import pandas as pd
dict_data = {'Treatment': ['C', 'C', 'C'], 'Biorep': ['A', 'A', 'A'], 'Techrep': [1, 1, 1], 'AAseq': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'], 'mz':[500.0, 500.5, 501.0]}
df_a = pd.DataFrame(dict_data)
dict_data = {'Treatment1': ['C', 'C', 'C'], 'Biorep1': ['A', 'A', 'A'], 'Techrep1': [1, 1, 1], 'AAseq1': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'], 'inte1':[1100.0, 1050.0, 1010.0]}
df_b = pd.DataFrame(dict_data)</code>
Copy after login

To concaten

The above is the detailed content of How to Combine Pandas DataFrames Side-by-Side?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template