Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Function on DataFrame Index

What is the best way to apply a function over the index of a Pandas DataFrame? Currently I am using this verbose approach:

pd.DataFrame({"Month": df.reset_index().Date.apply(foo)}) 

where Date is the name of the index and foo is the name of the function that I am applying.

like image 635
Alex Rothberg Avatar asked Nov 16 '13 23:11

Alex Rothberg


People also ask

How do you apply a function in a data frame?

DataFrame - apply() function. The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).

What is apply () in pandas?

The apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.

How do I apply a function to each column in pandas?

Python's Pandas Library provides an member function in Dataframe class to apply a function along the axis of the Dataframe i.e. along each row or column i.e. Important Arguments are: func : Function to be applied to each column or row. This function accepts a series and returns a series.


1 Answers

As already suggested by HYRY in the comments, Series.map is the way to go here. Just set the index to the resulting series.

Simple example:

df = pd.DataFrame({'d': [1, 2, 3]}, index=['FOO', 'BAR', 'BAZ']) df         d FOO     1 BAR     2 BAZ     3  df.index = df.index.map(str.lower) df         d foo     1 bar     2 baz     3 

Index != Series

As pointed out by @OP. the df.index.map(str.lower) call returns a numpy array. This is because dataframe indices are based on numpy arrays, not Series.

The only way of making the index into a Series is to create a Series from it.

pd.Series(df.index.map(str.lower)) 

Caveat

The Index class now subclasses the StringAccessorMixin, which means that you can do the above operation as follows

df.index.str.lower() 

This still produces an Index object, not a Series.

like image 122
firelynx Avatar answered Oct 02 '22 19:10

firelynx