How to add logic to list comprehension when renaming columns?

Viewed 78

I have dictionary that I am using to rename columns in a dataframe like so:

column_names = {name1:rename1, name2:rename2}
new_df = df[[k for k in column_names.keys()]]
new_df.rename(columns=columns_dict, inplace=True)

When a new field comes dictionary in I am getting this error at this line in the code:

code: new_df = df[[k for k in column_names.keys()]]
issue: KeyError: "['new_col'] not in index"

How do I create a flexible solution to the list comprehesion when renaming values, that if a value new value is present in the df or dictionary, include that column and assign it a value of zero?

I tried creating some try catches but I am not sure how to proceed after this point:

        try:
           new_df = df[[k for k in column_names.keys()]]
           new_df.rename(columns=columns_dict, inplace=True)
        except:
           #assign the failed column back to original df (named df) and assign value 
           #of zero
           #rerun all steps in try block.
  
2 Answers

Try changing this line of code

new_df = df[[k for k in column_names.keys() if k in df.columns]]

IIUC, you can divide the column_names into 2 parts: entries that are also in the columns of df and others. Then index & rename with the first part and assign with the second part:

# get the non-existent ones
to_assign = column_names.keys() - df.columns

# drop them
[column_names.pop(key) for key in to_assign]

# subset the `df` with what remained
ndf = df[column_names.keys()]

# rename them
ndf = ndf.rename(columns=column_names)

# assign the other ones (with zeros)
ndf = ndf.assign(**dict.fromkeys(to_assign, 0))

E.g.,

In []: df
Out[]:

   L_1  D_1  L_2   D_2
0  1.0    7  NaN   NaN
1  1.0   12  1-1  play
2  NaN   -1  1-1  play
3  1.0    9  1-1  play

In []: column_names = {"L_1": "M_1", "L_4": "Z_5", "D_1": "E_1"}

In []: ndf = above_operations...

In []: ndf
Out[]:

   M_1  E_1  L_4
0  1.0    7    0
1  1.0   12    0
2  NaN   -1    0
3  1.0    9    0
Related