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?
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.
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.
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.
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.
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
df1.append(df2.reindex_axis(df1.columns, 1, fill_value=0))
The advantage is that integer types should be preserved
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With