gpt4 book ai didi

python - 如何将现有 Pandas DataFrame 的所有值设置为零?

转载 作者:IT老高 更新时间:2023-10-28 21:07:11 28 4
gpt4 key购买 nike

我目前有一个带有日期索引的现有 Pandas DataFrame,每个列都有一个特定的名称。

对于数据单元格,它们填充了各种浮点值。

我想复制我的 DataFrame,但将所有这些值替换为零。

目标是重用 DataFrame 的结构(维度、索引、列名),但通过用零替换它们来清除所有当前值。

我目前实现的方式如下:

df[df > 0] = 0

但是,这不会替换 DataFrame 中的任何负值。

难道没有更通用的方法来用单个公共(public)值填充整个现有 DataFrame 吗?

提前感谢您的帮助。

最佳答案

同时保留 dtypes 的绝对最快方法如下:

for col in df.columns:
df[col].values[:] = 0

这直接写入每列的底层 numpy 数组。我怀疑任何其他方法都会比这更快,因为这不会分配额外的存储空间并且不会通过 pandas 的 dtype 处理。您还可以使用 np.issubdtype 仅将数字列清零。如果您有一个混合的 dtype DataFrame,这可能是您想要的,但如果您的 DataFrame 已经完全是数字,那么当然没有必要。

for col in df.columns:
if np.issubdtype(df[col].dtype, np.number):
df[col].values[:] = 0

对于小型 DataFrame,子类型检查的成本有些高。但是,将非数字列清零的成本很高,因此如果您不确定您的 DataFrame 是否完全是数字,您可能应该包括 issubdtype 检查。


时间比较

设置

import pandas as pd
import numpy as np

def make_df(n, only_numeric):
series = [
pd.Series(range(n), name="int", dtype=int),
pd.Series(range(n), name="float", dtype=float),
]
if only_numeric:
series.extend(
[
pd.Series(range(n, 2 * n), name="int2", dtype=int),
pd.Series(range(n, 2 * n), name="float2", dtype=float),
]
)
else:
series.extend(
[
pd.date_range(start="1970-1-1", freq="T", periods=n, name="dt")
.to_series()
.reset_index(drop=True),
pd.Series(
[chr((i % 26) + 65) for i in range(n)],
name="string",
dtype="object",
),
]
)

return pd.concat(series, axis=1)

>>> make_df(5, True)
int float int2 float2
0 0 0.0 5 5.0
1 1 1.0 6 6.0
2 2 2.0 7 7.0
3 3 3.0 8 8.0
4 4 4.0 9 9.0

>>> make_df(5, False)
int float dt string
0 0 0.0 1970-01-01 00:00:00 A
1 1 1.0 1970-01-01 00:01:00 B
2 2 2.0 1970-01-01 00:02:00 C
3 3 3.0 1970-01-01 00:03:00 D
4 4 4.0 1970-01-01 00:04:00 E

小数据帧

n = 10_000                                                                                  

# Numeric df, no issubdtype check
%%timeit df = make_df(n, True)
for col in df.columns:
df[col].values[:] = 0
36.1 µs ± 510 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

# Numeric df, yes issubdtype check
%%timeit df = make_df(n, True)
for col in df.columns:
if np.issubdtype(df[col].dtype, np.number):
df[col].values[:] = 0
53 µs ± 645 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

# Non-numeric df, no issubdtype check
%%timeit df = make_df(n, False)
for col in df.columns:
df[col].values[:] = 0
113 µs ± 391 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

# Non-numeric df, yes issubdtype check
%%timeit df = make_df(n, False)
for col in df.columns:
if np.issubdtype(df[col].dtype, np.number):
df[col].values[:] = 0
39.4 µs ± 1.91 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

大数据帧

n = 10_000_000                                                                             

# Numeric df, no issubdtype check
%%timeit df = make_df(n, True)
for col in df.columns:
df[col].values[:] = 0
38.7 ms ± 151 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# Numeric df, yes issubdtype check
%%timeit df = make_df(n, True)
for col in df.columns:
if np.issubdtype(df[col].dtype, np.number):
df[col].values[:] = 0
39.1 ms ± 556 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# Non-numeric df, no issubdtype check
%%timeit df = make_df(n, False)
for col in df.columns:
df[col].values[:] = 0
99.5 ms ± 748 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# Non-numeric df, yes issubdtype check
%%timeit df = make_df(n, False)
for col in df.columns:
if np.issubdtype(df[col].dtype, np.number):
df[col].values[:] = 0
17.8 ms ± 228 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

我之前建议过下面的答案,但现在我认为它是有害的——它比上面的答案慢得多,而且更难推理。它唯一的优点是写得更好。

The cleanest way is to use a bare colon to reference the entire dataframe.

df[:] = 0

Unfortunately the dtype situation is a bit fuzzy because every column in the resulting dataframe will have the same dtype. If every column of df was originally float, the new dtypes will still be float. But if a single column was int or object, it seems that the new dtypes will all be int.

关于python - 如何将现有 Pandas DataFrame 的所有值设置为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42636765/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com