df["year"] = df["timestamp"].apply(lambda x: f"{x:%Y}" if x is not pd.NaT else str(x))
然后:
print(df)
# Output
one two timestamp year
a 0.310610 -1.882089 NaT NaT
b -0.130122 0.633239 NaT NaT
c 0.251551 1.590384 2012-01-01 2012
d -1.635151 -0.123938 2012-01-01 2012
e 1.925423 1.142137 NaT NaT
1条答案
按热度按时间qnyhuwrf1#
具有以下玩具 Dataframe :
下面是一种方法:
然后: