[英]replace value if string ends with certain value pandas dataframe
As titled.如题。
I have written my code but it does not work.我已经编写了我的代码,但它不起作用。 I wish I can get a more pythonic way of writing the code (in one single line perhaps).
我希望我能获得一种更 Pythonic 的方式来编写代码(也许在一行中)。
clean_df
: clean_df
:
columnA
123F
FVGD
w999Z
678Q
6y6yA
My code:我的代码:
postfix = ["A", "D", "Z", "P"]
for value in postfix:
if cleaned_data['columnA'].str.endswith(value) is True:
cleaned_data['columnA'] = value
else:
cleaned_data['columnA'] = "blah"
The postfix
are constant. postfix
是不变的。 Expected outcome:预期结果:
columnA
blah
D
Z
blah
A
In one line with a list comprehension:在与列表理解的一行中:
postfix = ["A", "D", "Z", "P"]
cleaned_data['columnA'] = [value[-1] if value[-1] in postfix else "blah" for value in cleaned_data['columnA']]
The output is: output 是:
columnA
blah
D
Z
blah
A
You can try this with pd.Series.str.extract
here.您可以在此处使用
pd.Series.str.extract
进行尝试。
pat = "|".join(postfix)
pat = f"({pat}$"
df['columnA'] = df['columnA'].str.extract(pat, expand=False).fillna('blah')
df
columnA
0 blah
1 D
2 Z
3 blah
4 A
A simple one-liner would be一个简单的单线将是
df['columnA'] = np.where(df.columnA.str[-1].isin(postfix), df.columnA.str[-1], 'blah')
np.where
takes in a condition, value if the condition is True and value if the condition is False. np.where
接受一个条件,如果条件为真,则为 value,如果条件为假,则为 value。
OR或者
In pure pandas, without using numpy, it would be在纯 pandas 中,不使用 numpy,它将是
df['columnA'] = df.columnA.str[-1].where(df.columnA.str[-1].isin(postfix), 'blah')
You can use numpy.where
:您可以使用
numpy.where
:
Note: str.endswith
accepts a tuple
also.注意:
str.endswith
也接受一个tuple
。
In [3933]: import numpy as np
In [3934]: df.columnA = np.where(df.columnA.str.endswith(tuple(postfix)), df.columnA.str[-1], 'blah')
In [3935]: df
Out[3935]:
columnA
0 blah
1 D
2 Z
3 blah
4 A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.