Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

타임스탬프에서 차이점 찾기 – Python Pandas

<시간/>

타임스탬프의 차이를 찾기 위해 인덱스 연산자, 즉 대괄호를 사용하여 차이를 찾을 수 있습니다. 타임스탬프의 경우 abs()도 사용해야 합니다. 먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

3개의 열이 있는 DataFrame을 만듭니다. 타임스탬프가 있는 두 개의 날짜 열이 있습니다 -

dataFrame = pd.DataFrame(
   {
      "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"],

      "Date_of_Purchase": [
         pd.Timestamp("2021-06-10"),
         pd.Timestamp("2021-07-11"),
         pd.Timestamp("2021-06-25"),
         pd.Timestamp("2021-06-29"),
         pd.Timestamp("2021-03-20"),
      ],
      "Date_of_Service": [
         pd.Timestamp("2021-11-10"),
         pd.Timestamp("2021-12-11"),
         pd.Timestamp("2021-11-25"),
         pd.Timestamp("2021-11-29"),
         pd.Timestamp("2021-08-20"),
      ]
   })

이제 두 날짜 열의 타임스탬프 간의 차이를 알아보겠습니다. −

timestamp_diff = abs(dataFrame['Date_of_Purchase']-dataFrame['Date_of_Service'])

다음은 코드입니다 -

import pandas as pd

# create a dataframe with 3 columns
dataFrame = pd.DataFrame(
   {
      "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"],

      "Date_of_Purchase": [
         pd.Timestamp("2021-06-10"),
         pd.Timestamp("2021-07-11"),
         pd.Timestamp("2021-06-25"),
         pd.Timestamp("2021-06-29"),
         pd.Timestamp("2021-03-20"),
      ],
      "Date_of_Service": [
         pd.Timestamp("2021-11-10"),
         pd.Timestamp("2021-12-11"),
         pd.Timestamp("2021-11-25"),
         pd.Timestamp("2021-11-29"),
         pd.Timestamp("2021-08-20"),
      ]
   })

print"DataFrame...\n", dataFrame

# find difference in timestamps
timestamp_diff = abs(dataFrame['Date_of_Purchase']-dataFrame['Date_of_Service'])
print"\nDifference between two Timestamps: \n",timestamp_diff

출력

이것은 다음과 같은 출력을 생성합니다 -

DataFrame...
        Car   Date_of_Purchase   Date_of_Service
0      Audi         2021-06-10        2021-11-10
1     Lexus         2021-07-11        2021-12-11
2     Tesla         2021-06-25        2021-11-25
3  Mercedes         2021-06-29        2021-11-29
4       BMW         2021-03-20        2021-08-20

Difference between two Timestamps:
0   153 days
1   153 days
2   153 days
3   153 days
4   153 days
dtype: timedelta64[ns]