eriee eriee - 18 days ago 5
Python Question

How to transform the column values to another pattern in DataFrame, Python?

The values in 2nd-8th columns in DataFrame named

df
has a pattern like
{'close': 'time1', 'open': 'time2'}
, for example,
{'close': '21:00', 'open': '11:00'}
.

How can I change all the values that have this pattern to time2-time1, such as 11:00-21:00?

enter image description here

Answer

use the `apply function to call a custom algorithm to manipulate your dictionaries:

df.Monday.apply(lambda d: '-'.join([d['open'], d['close']]) 
                          if isinstance(d, dict) else d)