簡體   English   中英

將多個 DateTime 列合並為一個列(Python)

[英]Joining multiple DateTime columns into one Columns (Python)

我試圖將我所有的日期時間信息收集到一列中。 現在,我在 6 個月的時間段內都有一個列,但是為了進行時間序列分析,我試圖將所有日期時間收集到一個列中。

這是我累的地方:

 df['Dates'] = df["1 Month Date"]+ df["2 Month Date"]+ df["3 Month Date"] + df["4 Month Date"] + df["5 Month Date"] +df["6 Month Date"]

錯誤信息:

  TypeError: cannot add DatetimeArray and DatetimeArray

二審:

Dates = df["1 Month Date","2 Month Date","3 Month Date","4 Month Date","5 Month Date","6 Month Date"]

錯誤信息:

KeyError: ('1 Month Date', '2 Month Date', '3 Month Date', '4 Month Date', '5 Month Date', '6 Month Date')

額外信息:這是我使用熊貓導入的 excel 表,我的 1 個月日期 2 個月日期等..當我執行 df.info() 時是 datetime64[ns]

樣本數據:

   1 Month Date  1 Month Room Booked 2 Month Date  2 Month Room Booked  \
0    2020-09-01                  339   2020-10-01                  346   
1    2020-09-01                    2   2020-10-01                    4   
2    2020-09-01                    4   2020-10-01                    4   
3    2020-09-01                    0   2020-10-01                    0   
4    2020-09-01                    0   2020-10-01                    0   
5    2020-09-01                    1   2020-10-01                    1   
6    2020-09-01                    2   2020-10-01                    2   
7    2020-09-01                   50   2020-10-01                   58   
8    2020-09-01                   12   2020-10-01                   12   
9    2020-09-01                    9   2020-10-01                    9   
10   2020-09-01                    6   2020-10-01                    6   
11   2021-03-01                  112   2021-04-01                  112   
12   2021-03-01                    0   2021-04-01                    0   
13   2021-02-01                   36   2021-03-01                   36   
14   2021-02-01                   18   2021-03-01                   18   
15   2021-02-01                   20   2021-03-01                   20   
16   2021-02-01                   12   2021-03-01                   12   
17   2021-02-01                    0   2021-03-01                    0   

IIUC 用途:

Dates = df[["1 Month Date","2 Month Date","3 Month Date","4 Month Date","5 Month Date","6 Month Date"]].apply(pd.Series.explode).sum(axis=1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM