Pandas日期格式資料處理

2021-10-20 06:54:51 字數 1658 閱讀 1151

import numpy as np

import pandas as pd

df = pd.read_excel(

'成績表(統計).xls'

,sheet_name=

'寫入(舊)'

)df.head(

)# print(df.info())

## 轉換成日期格式

df['考試日期'

]= pd.to_datetime(df[

'日期'],

format

='%y.%m.%d'

,errors=

'coerce'

)# print(df)

## 提取年、月、日

df = df[

'考試日期'

].dt.year

df.head(

)print

(df)

df = df[

'考試日期'

].dt.month

df.head(

)print

(df)

df = df[

'考試日期'

].dt.day

df.head(

)print

(df)

## 計算時間差

df['上次考試日期差'

]= pd.datetime.now(

)- df[

'考試日期'

]df.head(

)print

(df)

df = df[

'上次考試日期差'

].dt.days

df.head(

)print

(df)

df = df[

'上次考試日期差'

].dt.seconds

df.head(

)print

(df)

## 轉化為天數

df['時間差'

]= df[

'上次考試日期差'

]/pd.timedelta(

'1 d'

)df[

'時間差'

].head(

)print

(df[

'時間差'

].head())

## 轉化為月數

df['時間差'

]= df[

'上次考試日期差'

]/pd.timedelta(

'1 m'

)df[

'時間差'

].head(

)print

(df[

'時間差'

].head())

##df[

'時間差'].

round

(decimals=3)

df['時間差'

].head(

)print

(df[

'時間差'

].head())

## 時間差轉化為天數

df = df[

'上次考試日期差'

].astype(

'timedelta64[m]'

)#d\s\h

print

(df.head(

))

pandas 資料處理

pandas中資料可以分為series,dataframe,panel分別表示一維至三維資料。其中在構造時,index表示行名,columns表示列名 構造方式 s pd.series data index index s pd series np random randn 5 index a b ...

pandas資料處理

dataframe.duplicated subset none,keep first 判斷dataframe中的資料是否有重複 必須一行中所有資料都重複才算重複,只能判斷行,不能判斷列 返回series dataframe.drop duplicates subset none,keep firs...

Pandas資料處理

資料處理 pandas from sklearn.preprocessing import minmaxscaler data 1,2 0.5,6 0.10 1,18 將 numpy 轉換成 pd 表 pd.dataframe data 歸一化 0,1 之間 scaler minmaxscaler ...