将列添加到df中,如果某个值为0,则返回1,否则返回该列的原始值



我试图实现此结果的Python代码是:

df['column2'] = np.where(df['column1'] == 0, 1, df['column1'])
  • 对于示例dataframe,使用np.where是最快的。
  • 您也可以使用pandas.DataFrame.where,它将替换条件为False的值,否则返回数据框列中的值。
  • 100用于使更新更容易看到
import pandas as pd
# test dataframe
df = pd.DataFrame({'a': [2, 4, 1, 0, 2, 2, 0, 8, 4, 0], 'b': [2, 4, 0, 9, 2, 0, 2, 8, 0, 3]})
# replace 0 with 100 or leave the same number based on the same column
df['0 → 100 on a if a'] = df.a.where(df.a != 0, 100)
# replace 0 with 100 or leave the same number based on a different column
df['0 → 100 on a if b'] = df.a.where(df.b != 0, 100)
# display(df)
a  b  0 → 100 on a if a  0 → 100 on a if b
0  2  2                  2                  2
1  4  4                  4                  4
2  1  0                  1                100
3  0  9                100                  0
4  2  2                  2                  2
5  2  0                  2                100
6  0  2                100                  0
7  8  8                  8                  8
8  4  0                  4                100
9  0  3                100                  0

%%timeit测试

测试数据

import pandas as pd
import numpy as np
# test dataframe with 1M rows
np.random.seed(365)
df = pd.DataFrame({'a': np.random.randint(0, 10, size=(1000000)), 'b': np.random.randint(0, 10, size=(1000000))})
测试

%%timeit
np.where(df.a == 0, 1, df.a)
[out]:
161 µs ± 1.47 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeit
np.where(df.b == 0, 1, df.a)
[out]:
164 µs ± 1.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeit
df.a.where(df.a != 0, 1)
[out]:
4.51 ms ± 136 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
df.a.where(df.b != 0, 1)
[out]:
4.55 ms ± 200 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
noah1(df)
[out]:
4.63 ms ± 58.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
noah2(df)
[out]:
15.3 s ± 205 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
paul(df)
[out]:
341 ms ± 34 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
karam(df)
[out]:
299 ms ± 4.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

功能
def noah1(d):
return d.a.replace(0, 1)
def noah2(d):
return d.apply(lambda x: 1 if x.a == 0 else x.b, axis=1)
def paul(d):
return [1 if v==0 else v for v in d.a.values]
def karam(d):
return d.a.apply(lambda x: 1 if x == 0 else x)

上面提供的应用程序示例应该可以工作,或者这个也可以:

df['column_2'] = [1 if v==0 else v for v in df['col'].values]

我的例子使用list comprehension: https://www.w3schools.com/python/python_lists_comprehension.asp

另一个答案使用lambda function: https://www.w3schools.com/python/python_lambda.asp

就我个人而言,在编写其他人可能使用的脚本时,我认为列表推导式更广为人知,因此更冗长,但我相信lambda函数执行得更快,并且通常是一个非常有用的工具,因此可能推荐上面的列表推导式。

你要做的就是复制这个列并用1代替0:

df["Column2"] = df["Column1"].replace(0,1)

更一般地说,如果您想要其他ColumnX中的值,您可以执行以下lambda函数:

df["Column2"] = df.apply(lambda x: 1 if x["Column1"]==0 else x['ColumnX'], axis=1)

您应该能够以这种方式使用apply语句实现这一点:

df['column2'] = df['column1'].apply(lambda x: 1 if x == 0 else x)

最新更新