使用lambda以字符串开头时

使用lambda以字符串开头时

本文介绍了使用lambda以字符串开头时,替换DataFrame列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame:

I have a DataFrame:

import pandas as pd
import numpy as np
x = {'Value': ['Test', 'XXX123', 'XXX456', 'Test']}
df = pd.DataFrame(x)

我想使用lambda将以XXX开头的值替换为np.nan.

I want to replace the values starting with XXX with np.nan using lambda.

我已经尝试了很多有关替换,应用和映射的事情,而我能做的最好的事情是False,True,True,False.

I have tried many things with replace, apply and map and the best I have been able to do is False, True, True, False.

下面的方法有效,但是我想知道一种更好的方法,我认为apply,replace和lambda可能是一种更好的方法.

The below works, but I would like to know a better way to do it and I think the apply, replace and a lambda is probably a better way to do it.

df.Value.loc[df.Value.str.startswith('XXX', na=False)] = np.nan

推荐答案

使用应用方法

In [80]: x = {'Value': ['Test', 'XXX123', 'XXX456', 'Test']}
In [81]: df = pd.DataFrame(x)
In [82]: df.Value.apply(lambda x: np.nan if x.startswith('XXX') else x)
Out[82]:
0    Test
1     NaN
2     NaN
3    Test
Name: Value, dtype: object

apply,where,loc的性能比较

Performance Comparision of apply, where, loc

这篇关于使用lambda以字符串开头时,替换DataFrame列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:59