在40万个 pandas 数据框中添加随机日期 [英] add random dates in 400K pandas dataframe

查看:65
本文介绍了在40万个 pandas 数据框中添加随机日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将第四列添加到长度为 465017 的以下数据框中。

Trying to append a fourth column to the following dataframe of length 465017.

     0        1     2
0   228055  231908  1
1   228056  228899  1

运行以下语法

x["Fake_date"]= fake.date(pattern="%Y-%m-%d", end_datetime=None)

返回

     0        1    2    Fake_date
0   228055  231908  1   1980-10-12
1   228056  228899  1   1980-10-12

但我想在<$ c上使用不同的随机日期$ c> 465017 实例行,

      0       1    2    Fake_date
0   228055  231908  1   1980-10-11
1   228056  228899  1   1980-09-12

如何我该随机化吗?

推荐答案

没有 faker 包,您可以执行以下操作:

Without the faker package, you can do this:

import numpy as np
import pandas as pd

x["Fake_date"] = np.random.choice(pd.date_range('1980-01-01', '2000-01-01'), len(x))

>>> x
        0       1  2  Fake_date
0  228055  231908  1 1999-12-08
1  228056  228899  1 1989-01-25

pd.date_range()中的2个日期字符串替换为您要选择的最小和最大日期始于

replacing the 2 date strings in pd.date_range() with the minimum and maximum date that you want to choose random dates from

这篇关于在40万个 pandas 数据框中添加随机日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆