如何在Pandas中的Datetime列中使用条件逻辑?

时间:2019-09-07 17:40:53

标签: python pandas date dataframe datetime

我有两个日期时间列-ColumnA和ColumnB。我想使用条件逻辑创建一个新列-ColumnC。

最初,我从YearMonth列(例如201907、201908等)创建了ColumnB。

当ColumnA为NaN时,我想选择ColumnB。 否则,我要选择ColumnA。

当前,我的下面代码使ColumnC具有不同的格式。我不确定如何摆脱所有这些0。我希望整列为YYYY-MM-DD。

    ID  YearMonth  ColumnA      ColumnB     ColumnC
0   1   201712     2017-12-29   2017-12-31  2017-12-29
1   1   201801     2018-01-31   2018-01-31  2018-01-31
2   1   201802     2018-02-28   2018-02-28  2018-02-28
3   1   201806     2018-06-29   2018-06-30  2018-06-29
4   1   201807     2018-07-31   2018-07-31  2018-07-31
5   1   201808     2018-08-31   2018-08-31  2018-08-31
6   1   201809     2018-09-28   2018-09-30  2018-09-28
7   1   201810     2018-10-31   2018-10-31  2018-10-31
8   1   201811     2018-11-30   2018-11-30  2018-11-30
9   1   201812     2018-12-31   2018-12-31  2018-12-31
10  1   201803     NaN          2018-03-31  1522454400000000000
11  1   201804     NaN          2018-04-30  1525046400000000000
12  1   201805     NaN          2018-05-31  1527724800000000000
13  1   201901     NaN          2019-01-31  1548892800000000000
14  1   201902     NaN          2019-02-28  1551312000000000000
15  1   201903     NaN          2019-03-31  1553990400000000000
16  1   201904     NaN          2019-04-30  1556582400000000000
17  1   201905     NaN          2019-05-31  1559260800000000000
18  1   201906     NaN          2019-06-30  1561852800000000000
19  1   201907     NaN          2019-07-31  1564531200000000000
20  1   201908     NaN          2019-08-31  1567209600000000000
21  1   201909     NaN          2019-09-30  1569801600000000000
df['ColumnB'] = pd.to_datetime(df['YearMonth'], format='%Y%m', errors='coerce').dropna() + pd.offsets.MonthEnd(0) 

df['ColumnC'] = np.where(pd.isna(df['ColumnA']), pd.to_datetime(df['ColumnB'], format='%Y%m%d'), df['ColumnA'])

2 个答案:

答案 0 :(得分:0)

df ['ColumnC'] = np.where(df ['ColumnA']。isnull(),df ['ColumnB'],df ['ColumnA'])

答案 1 :(得分:0)

就算了!

df['ColumnC'] = np.where(pd.isna(df['ColumnA']), pd.to_datetime(df['ColumnB']), pd.to_datetime(df['ColumnA']))