How can I change each element in a hierarchical indexed DataFrame? For example, maybe I want to convert strings to float:
from pandas import DataFrame f = DataFrame({'a': ['1,000','2,000','3,000'], 'b': ['2,000','3,000','4,000']}) f.columns = [['level1', 'level1'],['item1', 'item2']] f Out[152]: level1 item1 item2 0 1,000 2,000 1 2,000 3,000 2 3,000 4,000
I tried this:
def clean(group): group = group.map(lambda x: x.replace(',', '')) return group f.apply(clean) Out[153]: (level1, item1) (level1, item2) 0 1000 2000 1 2000 3000 2 3000 4000
As you can see, it changes hierarchical indexing a bit. How can i avoid this? Or maybe there is a better way.
thanks
Robert Smith
source share