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.
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).
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.
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.
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
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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With