Python:检查if条件的pandas列值的长度



我有一个处理多个文件的python程序。每个文件都有基于部门的客户id列。有些文件有8位客户id,有些文件有9位客户id。我需要做这个

if length of customer id column value is == 8:
input_file['customer_id'] = input_file['customer_id'].str[0:2] + '-' + input_file['customer_id'].str[2:8]
if length of customer id column value is == 9:
input_file['customer_id'] = 'K' + '-' + input_file['customer_id'].str[0:3] + '-' + input_file['customer_id'].str[3:8]

输入

id cusotmerid
1  89898988
2  898989889

输出

id cusotmerid
1  89-898988
2  K-898-989889

我怎么能做到呢?找不到任何能做这个的东西

您也可以这样做。

您可以使用内置lenpd.Series.map来查找列值的长度。有了它,您可以确定如何分配值。

使用astype(str)将数值转换为字符串,以便进行字符串串联。

input_file.loc[input_file['cusotmerid'].astype(str).map(len) == 8, 'new_customer_id'] = input_file['cusotmerid'].astype(str).str[:2]+ '-' + input_file['cusotmerid'].astype(str).str[2:]
input_file.loc[input_file['cusotmerid'].astype(str).map(len) == 9, 'new_customer_id'] = 'K-' + input_file['cusotmerid'].astype(str).str[:3] + '-' + input_file['cusotmerid'].astype(str).str[3:]

这应该是新的价值分配。

其输出为:

cusotmerid new_customer_id
0    89898988       89-898988
1   898989889    K-898-989889

您可以使用np.select。为了检查字符串的长度,您必须首先确保列的格式是字符串,因此是.astype(str)。然后,您可以使用.apply(lambda x: len(x) == condition)返回基于条件的结果:

import numpy as np
input_file['cusotmerid'] = input_file['cusotmerid'].astype(str)
input_file['cusotmerid'] = np.select([input_file['cusotmerid'].apply(lambda x: len(x) == 8),
input_file['cusotmerid'].apply(lambda x: len(x) == 9)],
[input_file['cusotmerid'].str[0:2] + '-' + input_file['cusotmerid'].str[2:8],
'K' + '-' + input_file['cusotmerid'].str[0:3] + '-' + input_file['cusotmerid'].str[3:9]],
input_file['cusotmerid'])
input_file
id  cusotmerid
0   1   89-898988
1   2   K-898-989889

np.select语句分解为条件和结果可能更容易。我传递的3个参数是条件、结果和默认值(如果不满足任何条件(。

input_file['cusotmerid'] = input_file['cusotmerid'].astype(str)
c1 = input_file['cusotmerid'].apply(lambda x: len(x) == 8)
c2 = input_file['cusotmerid'].apply(lambda x: len(x) == 9)
conditions = [c1, c2]
r1 = input_file['cusotmerid'].str[0:2] + '-' + input_file['cusotmerid'].str[2:8]
r2 = 'K' + '-' + input_file['cusotmerid'].str[0:3] + '-' + input_file['cusotmerid'].str[3:9]
results = [r1,r2]
input_file['cusotmerid'] = np.select(conditions, results, input_file['cusotmerid'])
input_file
id  cusotmerid
0   1   89-898988
1   2   K-898-989889

最新更新