Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing data in a dataframe with hierarchical indexing

How can I change every element in a DataFrame with hierarchical indexing? For example, maybe I want to convert strings into floats:

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 the hierarchical indexing quite a bit. How can I avoid this? Or maybe there is a better way.

Thanks

like image 939
Robert Smith Avatar asked Dec 24 '12 20:12

Robert Smith


People also ask

How do you change a value in a data frame from an index?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.

How do pandas use hierarchical indexes?

To make the column an index, we use the Set_index() function of pandas. If we want to make one column an index, we can simply pass the name of the column as a string in set_index(). If we want to do multi-indexing or Hierarchical Indexing, we pass the list of column names in the set_index().


1 Answers

Pass the axis option to the apply function:

In [265]: f.apply(clean, axis=1)
Out[265]:
  level1
   item1 item2
0   1000  2000
1   2000  3000
2   3000  4000

When both axes have hierarchical indices here's a workaround:

In [316]: f.index = [[1,2,3],[1,2,3]]

In [317]: f
Out[317]:
    level1
     item1  item2
1 1  1,000  2,000
2 2  2,000  3,000
3 3  3,000  4,000

In [314]: f.apply(clean, axis=1).reindex(f.index)
Out[314]:
    level1
     item1 item2
1 1   1000  2000
2 2   2000  3000
3 3   3000  4000
like image 160
Zelazny7 Avatar answered Sep 19 '22 20:09

Zelazny7