[Python] How Python Pandas perform subtraction in specific pattern of column?

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    There are NaN data in the Time column and has block of the data. I need to perform subtraction at specific pattern as mentioned. I tried with cumsun and diff but not able to get correct answer.

    Attempted Code:

    data = {'Time': {600: nan, 601: '2025-04-23 22:30:38,205', 602: '2025-04-23 22:35:08,482', 603: nan, 604: nan, 605: nan, 606: '2025-04-23 22:38:15,791', 607: '2025-04-23 22:40:15,603', 608: '2025-04-23 22:40:27,789', 609: '2025-04-23 23:40:29,255', 610: nan}, 'BatSOC': {600: nan, 601: '1', 602: '8', 603: nan, 604: nan, 605: nan, 606: '9', 607: '10', 608: '11', 609: '100', 610: nan}}

    df = pd.DataFrame(data)
    df['timestamp'] = pd.to_datetime(df['timestamp'])
    df['time_diff_minutes'] = df['timestamp'].diff(2).dt.total_seconds() / 60


    Expected output: [​IMG]

    Perform substration in each group of availabe data at every 2nd data to previous data

    Continue reading...

Compartilhe esta Página