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>
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>
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!