1

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
1
  • Are you sure those are the column names you want in the final output? Commented Jan 11, 2021 at 17:41

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Doing this with larger datasets, I got: PerformanceWarning: DataFrame is highly fragmented. This is usually the result of calling frame.insert many times, which has poor performance. Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use newframe = frame.copy() self[col] = value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.