python - 如何用 1 和 0 替换时间戳?

标签 python pandas numpy

我想在大型数据集上用 1 替换注册时间日志,用 0 替换空单元格,下面是一个示例:

data = [['tom', '10', "2014-02-05 21:24:44 UTC"], ['nick', '',''], ['juli', 14, '2014-02-15 21:55:43 UTC']]

BD = pd.DataFrame(data, columns = ['Name', 'Age', 'Enrolled_at'])

我尝试了以下代码,但它们用于替换某个值,并且在我的日期集中,时间戳不是唯一的。

1

BD['enrolled_at'].replace('', "1", inplace=True)
BD.head()

2

BD.loc[(BD['enrolled_at'] > 1990)] = 1

3

BD['enrolled_at'].replace("$20$", "1", regex=True, inplace=True)
BD

.

e现状

The current situation

.

预期结果

Expected result

最佳答案

BD['Enrolled_at'] = pd.to_datetime(BD['Enrolled_at'])

BD['Enrolled_at'] = np.where(BD['Enrolled_at'] > '1990-01-01', 1, 0)

您可以将 1990 年日期设置为数据中日期的最低值

关于python - 如何用 1 和 0 替换时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696446/

相关文章:

python - 在 Python 中向下舍入日期时间对象

numpy - 如何使 scipy.ndimage.zoom 的 UserWarning 静音

python - 将 numpy 数组作为行添加到 pandas 中,并以字符串作为索引

python - 使用 Python 将列表导出为 CSV

python - 在 Pandas 中使用 fillna() 和 lambda 函数替换 NaN 值

python - 将多索引 Pandas 系列和 DataFrame 相乘

python - 为什么一个 352GB 的 NumPy ndarray 可以用在 8GB 内存的 macOS 电脑上?

python - imread在Spyder中有效,但在VS Code中无效

python - 在 R 和 python 中运行 Keras

Python 对象历史