Reputation: 1231
I have a dataframe whose column names are a subset of the index values of another dataframe.
I would like to add to the initial dataframe empty columns that exists in the second index but do not exist as column names in the first. I have solved this using the following loop but wonder is there a smarter way to do this using, for example, assign
or append(axis=1)
?
My example below with the output it produces (the correct desired result)
Thanks
import pandas as pd
df_matrix = pd.DataFrame([(1, 2),
(3, 4)],
index=[['a','b']],
columns=('value_1','value_3'))
df_matrix
value_1 | value_3 | |
---|---|---|
a | 1 | 2 |
b | 3 | 4 |
df_column = pd.DataFrame({'value':[11, 22, 33, 44]}, index=['value_1','value_2','value_3','value_4'])
df_column
value | |
---|---|
value_1 | 11 |
value_2 | 22 |
value_3 | 33 |
value_4 | 44 |
for index, row in df_column.iterrows():
if index not in df_matrix.columns:
df_matrix[index]=0
updated df_matrix
value_1 | value_3 | value_2 | value_4 | |
---|---|---|---|---|
a | 1 | 2 | 0 | 0 |
b | 3 | 4 | 0 | 0 |
Upvotes: 1
Views: 389
Reputation: 16172
This would essentially do what you want without the loop.
df_matrix[[x for x in df_column.index if x not in df_matrix.columns]] = 0
value_1 value_3 value_2 value_4
a 1 2 0 0
b 3 4 0 0
Upvotes: 3