日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

pandas中的Timestamp只保留日期不顯示時(shí)間_python

作者:翻山越嶺的豬 ? 更新時(shí)間: 2022-09-08 編程語(yǔ)言

Timestamp只保留日期不顯示時(shí)間

Timestamp.date()

拿到DataFrame中的一個(gè)時(shí)間戳后,加一個(gè)**.date()**即可

for time in df['日期']):
? ? print(time.date())

pandas從日期屬性中提取年月日

在數(shù)據(jù)挖掘過(guò)程中,日期屬性是非數(shù)值屬性, 不能直接輸入到模型,將日期屬性拆分成年、月和日是必要的。

date屬性是object類型的, 通過(guò)取單元格可以發(fā)現(xiàn)它是字符串類型,這樣很容易提取年、月、日

將日期屬性拆分成年、月、日

代碼如下:

def DateSplit(df, col):
    """
    split the object of '2010-01-02' into year(2010), month(1) and day(2).
    :param df:  to operate data (type:DataFrame)
    :param col: column label of date object (type:str)
    :return: converted date (type: DataFrame)
    """
    year, month, day = [], [], []
    data = df.loc[:, col].values
    df = df.drop([col], axis=1)
    
    for i in range(data.shape[0]):
        year.append(int(data[i][:4]))
        month.append(int(data[i][5:7]))
        day.append(int(data[i][8:]))
    date = pd.DataFrame({'year': year, 'month': month, 'day': day})
    result = pd.concat([date, df], axis=1)
    return result
 
 
pm25_train = pd.read_csv("./datasets_PM25/pm25_train.csv")
data= DateSplit(df=pm25_train,col='date')
data.head(10)

原文鏈接:https://blog.csdn.net/piggy0306/article/details/116015054

欄目分類
最近更新