如何將python中的數據框列對象轉換為日期時間格式 (How to convert a dataframe column object in python to date time format)


問題描述

如何將python中的數據框列對象轉換為日期時間格式 (How to convert a dataframe column object in python to date time format)

如何使用 pandas 將數據框中對像數據類型的列值轉換為 python 中的日期時間格式?

給出下面我得到的樣本值,當我執行 print(df['timestamp']) 以供參考

0 2020‑02‑03 18:00:33
1 2020‑02‑03 18:00:37
2 2020‑02‑03 18:00:39
br /> 3 2020‑02‑03 18:01:16
4 2020‑02‑03 18:01:17
5 2020‑02‑03 18:02:14
6 2020 ‑02‑03 18:02:46
7 2020‑02‑03 18:02:50
8 2020‑02‑03 18:02:58

請注意

  • 列時間戳的數據類型為對象


參考解法

方法 1:

df['ColumnName'] = pd.to_datetime(df['ColumnName'])

(by SunishawfullyCold)

參考文件

  1. How to convert a dataframe column object in python to date time format (CC BY‑SA 2.5/3.0/4.0)

#datetime-format #Python #pandas






相關問題

給定一個 DateTime 對象,如何獲取字符串格式的 ISO 8601 日期? (Given a DateTime object, how do I get an ISO 8601 date in string format?)

Праграма WPF наладжвае кароткую дату пры выкарыстанні stringformat={}{0:d} (WPF application customize shortdate when using stringformat={}{0:d})

使用 java.time.DateTimeFormatter 格式化 java Date,包括時間偏移 (Formating java Date with java.time.DateTimeFormatter including time offset)

# 如何使用 DateTime 類(處理轉換、格式、差異和時區) (# How to use the DateTime class (Dealing with Conversions, Formatting, Diffs, and Time zones))

從一種時間格式轉換為另一種時間格式 (Converting from one time format to the other)

如何將python中的數據框列對象轉換為日期時間格式 (How to convert a dataframe column object in python to date time format)

BeautifulSoup:如何以 datwtime 格式獲取 youtube 視頻的發布日期時間? (BeautifulSoup: How to get publish datetime of a youtube video in datwtime format?)

如何在 C# 中轉換 dateTime 格式? (How do I convert dateTime format in C#?)

在 SQL 中的時間範圍之間獲取記錄的替代方法(計算財政年度) (Alternative ways to get records between a time frame in SQL (calculate fiscal year))

Elasticsearch 自定義日期時間格式,包括。序數 (Elasticsearch custom date time format incl. ordinal numbers)

我有一個 int 格式的日期 DDMMYYYY,我怎樣才能分隔日月年 (I have a date in int format DDMMYYYY, how can I separate day and month and year)

Intl.DateTimeFormat 中 dayPeriod 的參考 (Reference for dayPeriod in Intl.DateTimeFormat)







留言討論