PANDAS Substitute non null column values with key value pairs of a dict

By Pearl

Suppose I have a dataframe:

       AC BC AK BJ  SDFG.... (1100 such columns)
Tom    0  0  1   0   0
Sally  1  0  1   1   0
Nick   0  1  0   0   1

I also have a dict where
dict_1= {‘AC’:0.23,’BC’:1.2, ‘AK’:0.45, ‘BJ’: 2.2, ‘ABFK’:4.5….}
it’s not necessary that all keys in dict1 are columns of df1, the total number of key value pairs may be 10,000, and columns are only 1100. SO significant number of keys are not columns like for ‘ABFK’ key is not there in df1
Similarly there might be column names which are not keys in dict_1. For e.g ‘SDFG’ column header has no associated key in dict.

I would like to return a dataframe where in dict1 the value has been substituted if the key exists as a column name:
for e.g what I would like to get back is

      AC    BC   AK    BJ    SDFG.... (1100 such columns) TOTAL
Tom    0    0    0.45   0     0                             0.45
Sally  0.23 0    0.45   2.2   0                             2.7
Nick   0    1.2  0      0     0.13                          1.33

So all(1s) have been substituted by values based on dictionary . If the key didn’t exist like SDFG it is changed to a default value of 0.13. In the end I would like to have a total column name which sums across the row.

What would be an easy PANDAS solution for this?
Thanks a lot to all those who would help a novice out.

Source: Stack Overflow


Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273