1

I have dataframe with two columns time and dic in dic column i want to drop 1:00 and 4:00. how to do that with pandas

import pandas as pd
dic={'time':[1685660515,1685689306],
     'dic':[{"0:00":"2", "1:00":"1","2:00":"0","4:00":"0"},
            {"0:00": "0", "1:00": "0", "2:00":"3","4:00":"0" }]}
df= pd.DataFrame(dic)
time dic
1685660515 "0:00": "2", "1:00": "1", "2:00": "0","4:00":"0"
1685689306 "0:00": "0", "1:00": "0", "2:00":3","4:00":"0"
zircon
  • 742
  • 1
  • 10
  • 22
  • 1
    please provide a reproducible input, it's currently unclear whether you have strings, dictionaries, etc. and difficult to replicate. – mozway Jun 02 '23 at 07:45
  • 1
    If you really have dictionaries, you have to loop over the rows and over the keys to `del` or `pop` them. Something like: `for d in df['dic']: for k in d: if k in {'9:00', '15:00'}: del d[k]` – mozway Jun 02 '23 at 07:47
  • Unfortunately it's still not reproducible, please [read this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), and have you tried the duplicate and my suggestion above? – mozway Jun 02 '23 at 08:24
  • I have edited the quation as you said please check it once Thank you.@mozway – zircon Jun 02 '23 at 08:33
  • Then what I suggested above [works fine](https://i.stack.imgur.com/91Czm.png) – mozway Jun 02 '23 at 08:45
  • getting this error `RuntimeError: dictionary changed size during iteration` @mozway – zircon Jun 02 '23 at 08:58
  • 1
    Yes, replace `for k in d` by `for k in list(d)` – mozway Jun 02 '23 at 09:03

0 Answers0