Process time data configured with a.m. in Python

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

import numpy as np
import pandas as pd
from pandas import DataFrame


AM = pd.read_csv('C:\EDA/OSHA Time(am).csv')

Ghost=AM[['Time','Part of Body','Degree of Injury']]


I don't know if it's right to post it like this because it's my first time, but I can't find the content I want, so I The result I want is to sort out the number and type of the same part of the body at the same time, whether their status is fatal or non-fatal.

The time column of the existing csv file is all different without a certain standard, so 0x:00 a through Excel operation.I made a form like m, but I think it became ambiguous to write the conditions.

In my head, I declare a temporary variable and include it from 0x:00 to 0x:59 to give conditions using the variable, but it feels difficult to give a range because it is declared a.m. Is it okay if I ask for an answer?

python time dataframe

2022-09-20 14:44

1 Answers

Do you want this kind of thing?

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

>>> import pandas as pd
>>> df = pd.DataFrame({"Time":["01:00 a.m", "02:00 p.m"]})
>>> df
0  01:00 a.m
1  02:00 p.m

>>> def parse_ampm(s):
    tm_str, ampm = s.split(" ")
    h, m = tm_str.split(":")
    h, m = int(h), int(m)
    if ampm == "p.m":
        h += 12
    return "%02d:%02d"%(h, m)

>>> df["Time2"] = df["Time"].apply(parse_ampm)
>>> df
        Time  Time2
0  01:00 a.m  01:00
1  02:00 p.m  14:00

2022-09-20 14:44

If you have any answers or tips

© 2022 pinfo. All rights reserved.