Skip to content Skip to sidebar Skip to footer

Efficiently Creating Additional Columns In A Pandas Dataframe Using .map()

I am analyzing a data set that is similar in shape to the following example. I have two different types of data (abc data and xyz data): abc1 abc2 abc3 xyz1 xyz2 xyz3 0

Solution 1:

You can use applymap with the dictionary get method:

In[11]: df[abc_columns].applymap(categories.get)
Out[11]:
   abc1abc2abc30GoodBadBad1BadGoodGood2BadBadGood3GoodBadGood4GoodGoodBad

And put this to the specified columns:

In [12]: abc_categories = map(lambda x: x + '_category', abc_columns)

In [13]: abc_categories
Out[13]: ['abc1_category', 'abc2_category', 'abc3_category']

In [14]: df[abc_categories] = df[abc_columns].applymap(categories.get)

Note: you can construct abc_columns relatively efficiently using a list comprehension:

abc_columns = [col for col in df.columns if str(col).startswith('abc')]

Post a Comment for "Efficiently Creating Additional Columns In A Pandas Dataframe Using .map()"