eriee eriee - 1 year ago 79
Python Question

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

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

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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download