[英]make a copy of a string column and cut the string based on certain value
I have a DataFrame with a column with installation KKS-codes in Python.我有一个 DataFrame,在 Python 中有一列安装 KKS 代码。
The KKS-codes look like this: KKS 代码如下所示:
1BLA43AA030 1BOR53AR021 1BHY28UI021
1BLA43AA030 1BOR53AR021 1BHY28UI021
I want to make a new column where the string only has the relevant information.我想创建一个新列,其中字符串仅包含相关信息。 sometimes the code requires a number but it usually doesn't.
有时代码需要一个数字,但通常不需要。 The required number is given after the 3digit letter which specify the certain object. like this:
所需的号码在指定特定 object 的 3 位字母后给出。像这样:
BLA BOR BHY2
BLA BOR BHY2
I cut the full KKS-codes with我用
df_1['KKS'] = df_1.Object.str[1:4]
but for certain strings i need it to be但对于某些字符串,我需要它
df_1['KKS'] = df_1.Object.str[1:5]
My if-statements don't work, please help我的 if 语句不起作用,请帮助
I dont fully understand what you mean by我不完全明白你的意思
The required number is given after the 3digit letter which specify the certain object.
所需号码在指定特定 object 的 3 位字母后给出。
If you can explain this further with examples I can help more.如果您可以通过示例进一步解释这一点,我可以提供更多帮助。 Otherwise, this is how you can apply a function to a row in a dataframe:
否则,这就是如何将 function 应用于 dataframe 中的一行:
import pandas as pd
def test_for_four(s: str) -> bool:
third_digit_letter = s[4]
if third_digit_letter != "2":
return True
return False
def split_kks_code(s: str) -> str:
if test_for_four(s):
return s[1:4]
return s[1:5]
df = pd.DataFrame([{'KKS-Code': '1BLA43AA030'},
{'KKS-Code': '1BOR53AR021'},
{'KKS-Code': '1BHY28UI021'}])
df['KKS'] = df['KKS-Code'].apply(split_kks_code)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.