Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two frames with different number of columns in pandas?

Tags:

python

pandas

I have the following Dataframes:

Dataframe 1:

|---------------------|------------------|
|      property_id    |        beds      |
|---------------------|------------------|
|          1          |         1        |
|---------------------|------------------|
|          2          |         2        | 
|---------------------|------------------|

Dataframe 2:

|---------------------| 
|      property_id    |
|---------------------|
|          3          |
|---------------------|
|          4          |
|---------------------|

What I want to produce is the following Dataframe:

|---------------------|------------------|
|      property_id    |        beds      |
|---------------------|------------------|
|          1          |         1        |
|---------------------|------------------|
|          2          |         2        | 
|---------------------|------------------|
|          3          |         0        |
|---------------------|------------------|
|          4          |         0        | 
|---------------------|------------------|

What I want is to concatenate two Dataframes, and the former has more columns than the latter, but all the columns of the latter are in the former. When the column is not present in the latter dataframe I want to set a default value of 0. How can I achieve this?

df1 = pd.DataFrame({'property_id': [1, 2], 'beds': [1, 2]})
df2 = pd.DataFrame({'property_id': [3, 4]})

I have almost no experience with pandas, so what could I do?

like image 875
lmiguelvargasf Avatar asked Apr 24 '17 02:04

lmiguelvargasf


People also ask

How do you concatenate two DataFrames with different number of columns?

It is possible to join the different columns is using concat() method. DataFrame: It is dataframe name. axis: 0 refers to the row axis and1 refers the column axis. join: Type of join.

How concatenate two columns of different DataFrames in pandas?

The concat() function in pandas is used to append either columns or rows from one DataFrame to another. The concat() function does all the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.

How do I combine two data frames with different number of rows?

Use the full_join Function to Merge Two R Data Frames With Different Number of Rows. full_join is part of the dplyr package, and it can be used to merge two data frames with a different number of rows.

Can you merge two DataFrames of different lengths pandas?

It can be done using the merge() method. Below are some examples that depict how to merge data frames of different lengths using the above method: Example 1: Below is a program to merge two student data frames of different lengths.


2 Answers

You can use pandas.concat or append method for this, both methods will generate NA for columns that don't exist in the sub data frame, to fill them with zero, you can use fillna(0):

df1.append(df2).fillna(0)

#  beds     property_id
#0  1.0          1
#1  2.0          2
#0  0.0          3
#1  0.0          4


pd.concat([df1, df2]).fillna(0)

#  beds     property_id
#0  1.0         1
#1  2.0         2
#0  0.0         3
#1  0.0         4
like image 90
Psidom Avatar answered Oct 16 '22 21:10

Psidom


df1.append(df2.reindex_axis(df1.columns, 1, fill_value=0))

The advantage is that integer types should be preserved

like image 30
piRSquared Avatar answered Oct 16 '22 21:10

piRSquared