pythonpython-2.7pandasdivmod

Python divmod for every value in pandas Dataframe


I've got a pandas dataframe that looks like this:

   P-101  P-103  P-104  P-107  P-114  P-120
P   2415   2535   3345   5650   2805   6210
S      0     45   3105   1165      0      0
D      0    690    690    570    255    830

I want to apply a divmod(value, 60) to each cell, then format the result as [quotient]h[remainder]m, like this: 5h30m

I've tried:

df.values.apply(lambda x: divmod(x,60))

But that throws out an AttributeError

How can I apply divmod to every value?


Solution

  • You're looking for applymap, which applies the function element-wise rather than row- or column-wise in the case of apply:

    df.applymap(lambda x: divmod(x,60))