如何将 .timestamp 属性应用于列?

How to apply .timestamp attribute to a column?

import pandas as pd

df=pd.DataFrame(data=[[pd.Timestamp.now()],[pd.Timestamp.now()],[pd.Timestamp.now()],[pd.Timestamp.now()]],columns=['Date'])
    
df
                        Date
0 2022-01-19 12:15:40.801133
1 2022-01-19 12:15:40.801192
2 2022-01-19 12:15:40.801202
3 2022-01-19 12:15:40.801210

df.Date[0]
Timestamp('2022-01-19 12:15:40.801133')

df.Date[0].timestamp()
1642594540.801133

如何将 .timestamp 属性应用于数据框的整个列? 谢谢。

您可以使用 apply():

df["Date"].apply(lambda x: x.timestamp())

输出:

0    1.642594e+09
1    1.642594e+09
2    1.642594e+09
3    1.642594e+09
Name: Date, dtype: float64

或者,如果您想更改现有列:

df["Date"] = df["Date"].apply(lambda x: x.timestamp())

df

    Date
0   1.642594e+09
1   1.642594e+09
2   1.642594e+09
3   1.642594e+09