Bob
Bob

Reputation: 453

Add new values and columns inplace

I have a data frame that look like this:

         Front jvol  Back jvol    
row col                           
99  49        5734        5850    
    50        5735        5851    
    51        5736        5852    
    52        5737        5853 

and dictionary like:

dict =   
{5734: 1,
 5735: 0,
 5736: 1,
 5737: 1,
 5850: -1,
 5851: 0,
 5852: -1,
 5853: -1} 

I am trying to run the dictionary over each column values and return the results in new columns like this:

         Front jvol    Back jvol   Front jvol_d  Back jvol_d  
row col                                                 
99  49        5734        5850          1            -1  
    50        5735        5851          0             0  
    51        5736        5852          1            -1  
    52        5737        5853          1            -1

I tried this:

columns = ['Front jvol', 'Back jvol']
columns_d = ['Front jvol_d', 'Back jvol_d']
df = pd.concat([df, pd.DataFrame(columns = columns_d)])

for col_d, col in zip(columns_d, columns):
    for ivol in range(len(df[col])):
        try:
            df[col_d][ivol] = dict[df[col][ivol]]
        except:
            pass

But the results is strange. It changes the format of the other columns and gave SettingWithCopyWarning warning too:

SettingWithCopyWarning:    
A value is trying to be set on a copy of a slice from a DataFrame
    
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
      df[col_d][ivol] = dict[df[col][ivol]]


         Front jvol    Back jvol   Front jvol_d  Back jvol_d  
row col                                                 
99  49     5734.0        5850.0          1            -1  
    50     5735.0        5851.0          0             0  
    51     5736.0        5852.0          1            -1  
    52     5737.0        5853.0          1            -1 

Upvotes: 1

Views: 51

Answers (1)

jezrael
jezrael

Reputation: 863531

First rename variable dict, because python code word to d or dict1, then use DataFrame.replace - if no match get original value:

d =   
{5734: 1,
 5735: 0,
 5736: 1,
 5737: 1,
 5850: -1,
 5851: 0,
 5852: -1,
 5853: -1} 

df = df.join(df.replace(d).add_prefix('_d'))

Or Series.map - if no match value get NaN:

df = df.join(df.stack().map(d).unstack().add_prefix('_d'))

If values always matching dict both solution working with same output.

Upvotes: 3

Related Questions