从 python 中的“196 天 00:01:45”格式中删除天数

Strip number of days from "196 days 00:01:45" format in python

我正在尝试根据天数过滤掉我的数据框。我希望它超过 5 天。

x = df['gift_date'] - min(df['gift_date'])

我得到的输出是:

6213959   196 days 00:01:45
6213960   196 days 00:01:48
6213961   197 days 00:01:49
6213962   196 days 00:01:48
6213963   196 days 00:01:48
6213964   197 days 00:01:50
Name: invitation_date, Length: 6213965, dtype: timedelta64[ns]

我只想要这个结果的天数。

还有其他流程吗?

您可以为此使用 .dt.days,所以在您的情况下 x = (df['gift_date'] - min(df['gift_date'])).dt.days:

In [69]: s = pd.Series(pd.timedelta_range(0, 1e15, periods=10))

In [70]: s
Out[70]:
0           0 days 00:00:00
1    1 days 06:51:51.111111
2    2 days 13:43:42.222222
3    3 days 20:35:33.333333
4    5 days 03:27:24.444444
5    6 days 10:19:15.555555
6    7 days 17:11:06.666666
7    9 days 00:02:57.777777
8   10 days 06:54:48.888888
9          11 days 13:46:40
dtype: timedelta64[ns]

In [71]: s.dt.days
Out[71]:
0     0
1     1
2     2
3     3
4     5
5     6
6     7
7     9
8    10
9    11
dtype: int64