python - 根据条件 pandas 数据框列删除字符串

标签 python string pandas dataframe

我有以下数据:

df = pd.DataFrame({ 'Column_A': [1,2,3,4],
                'Column_B': [["X1", "X2", "Y1"],
                            ["X3", "Y2"],
                            ["X4", "X5"],
                            ["X5", "Y3", "Y4"]],})

   Column_A      Column_B
0         1  [X1, X2, Y1]
1         2      [X3, Y2]
2         3      [X4, X5]
3         4  [X5, Y3, Y4]

我希望删除第二列中所有以 Y 开头的字符串。期望的输出:

   Column_A  Column_B
0         1  [X1, X2]
1         2      [X3]
2         3  [X4, X5]
3         4      [X5]

最佳答案

使用嵌套列表理解和 startswith 进行过滤:

df['Column_B'] = [[y for y in x if not y.startswith('Y')] for x in df['Column_B']]

应用替代方案:

df['Column_B'] = df['Column_B'].apply(lambda x: [y for y in x if not y.startswith('Y')])

或者使用过滤器:

df['Column_B'] = [list(filter(lambda y: not y.startswith('Y'), x)) for x in df['Column_B']]
<小时/>
print (df)
   Column_A  Column_B
0         1  [X1, X2]
1         2      [X3]
2         3  [X4, X5]
3         4      [X5]

性能:

取决于行数、列表中的值的数量以及匹配值的数量:

#[40000 rows x 2 columns]
df = pd.concat([df] * 10000, ignore_index=True)
#print (df)


In [142]: %timeit df['Column_B'] = [[y for y in x if not y.startswith('Y')] for x in df['Column_B']]
23.7 ms ± 410 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [143]: %timeit df['Column_B'] = [list(filter(lambda y: not y.startswith('Y'), x)) for x in df['Column_B']]
36.5 ms ± 204 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [144]: %timeit df['Column_B'] = df['Column_B'].apply(lambda x: [y for y in x if not y.startswith('Y')])
30.4 ms ± 1.86 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

关于python - 根据条件 pandas 数据框列删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445542/

相关文章:

python - 使用 iterrows() 时的持久化问题

python - 如何拦截实例方法调用?

Python:从 ADLS 文件夹中查找重命名并移动 JSON 文件

python - 更改 is_superuser Django 的 verbose_name

java - 作为 byte[] 发送的字符串内容将其内容转义为查询字符串

string - 将字符添加到数据集中的字符串子集

c - 如何计算C中相同字符的个数?

python - 合并两个不完全匹配时间戳的 pandas 数据帧

python - Pandas 数据帧 : Replace based on filter and regex extract

python - 读取进程并与 dask 并行连接 pandas 数据帧