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.
Solution 1:
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.
Solution 2:
Assuming that you want to make a column in you're current DataFrame by applying your function "foo" to the index. You could write...
df['Month'] = df.index.map(foo)
To generate the series alone you could instead do ...
pd.Series({x: foo(x) for x in foo.index})