Apply Function on DataFrame Index

后端 未结 4 1851
孤独总比滥情好
孤独总比滥情好 2021-01-31 01:22

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\":          


        
4条回答
  •  清歌不尽
    2021-01-31 01:39

    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})
    

提交回复
热议问题