Home > Enterprise >  Build a DataFrame with given set of columns while copying value from another DataFrame with duplicat
Build a DataFrame with given set of columns while copying value from another DataFrame with duplicat

Time:12-18

Say I am given a DataFrame df1 with columns ['A', 'B', 'C'], and another list of columns labels, like cols = ['A', 'B', ..., 'Z'], among which the label may or may not also be column of df1. My goal is to build a DataFrame df with columns of cols, and if any column label in df1 also exists in cols, this column will be copied into df. My solution is like:

df = pd.DataFrame([], columns=cols)
for c in cols:
    if c in df1:
        df[c] = df1[c]

This works, but I am wondering if there is any more pandas-ish way to accomplish it. I don't mind the values of other columns to be NaN.

CodePudding user response:

I think you are looking for reindex:

df = df1.reindex(columns=cols)
  • Related