'How to check pandas column names and then append to row data efficiently?

I have a dataframe with several columns, some of which have names that match the keys in a dictionary. I want to append the value of the items in the dictionary to the non null values of the column whos name matches the key in said dictionary. Hopefully that isn't too confusing.

example:

realms = {}
realms['email'] = '<email>'
realms['android'] = '<androidID>'
df = pd.DataFrame()
df['email'] = ['[email protected]','',[email protected]]
df['android'] = [1234567,,55533321]

how could I could I append '<email>' to '[email protected]' and '[email protected]' without appending to the empty string or null value too?

I'm trying to do this without using iteritems(), as I have about 200,000 records to apply this logic to.

expected output would be like '[email protected]<email>',,'[email protected]<email>'



Solution 1:[1]

for column in df.columns:
    df[column] = df[column].astype(str) + realms[column]
>>> df
                  email              android
0  [email protected]<email>   1234567<androidID>
1  [email protected]<email>  55533321<androidID>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Orb