Facing Issue with DATAFRAME

I have written down below code to add few columns dynamically in a data frame. I am adding these columns in df_new dataframe but not sure why these new columns are being added up in df_wonullcol data frame as well. Below is the code screenshot for your reference.


df_new = df_wonullcol

col_data = []
for i in df_wonullcol.columns:
    if i.find('_MP')!=-1:
        for j in range(len(df_wonullcol[i].index)):
            col_data = df_wonullcol.loc[j,i]
            col_data = col_data.split(',')
            for x in col_data:
                df_new.loc[j,str('Q1_MP_'+x)] = x

Assignment doesn’t copy, so def_new is referring to the same object as df_wonullcol.

Try df_new = df_wonullcol.copy().


Thanks Matthew, your suggestion is of great help.
Also do you think that the code which i have written is efficient or is there any better way to do this?

  1. You have col_data = [], but it’s pointless because you never use that value, only assign to col_data later on.

  2. i is not a good name for a column. Such names are really only used when, say, you’re iterating over a range and i is an integer.

  3. '_MP' in i would be clearer than i.find('_MP') != -1.

  4. The result of 'Q1_MP_' + x is already a string, so you don’t need the str(...) around it.

As for efficiency, that depends on how big the dataframe is. If the code is already fast enough for its purpose, don’t worry about it.

1 Like

Thanks for sharing your suggestions here, i will try to refine my code by following your inputs.