我是否可以使用python函数对零售银行交易进行分组?

2024-05-23 19:18:16 发布

您现在位置:Python中文网/ 问答频道 /正文

Python中是否有其他函数可以用来对客户的事务进行分组?假设一个事务中包含一个特定的词,并且有多个具有相同名称的事务,然后将它们组合在一起。你知道吗

我用了这个代码,但它太长了,因为我有来自不同商家的数千个独特的交易。你知道吗

temp=tranx.TRANX.fillna("0")
tranx['Activity_2'] = pd.np.where(temp.str.contains("PNP "),"PICKNPAY",
               pd.np.where(temp.str.contains("CHECKERS"), "CHECKERS",
               pd.np.where(temp.str.contains("MRPRICE"), "MRPRICE",
               pd.np.where(temp.str.contains("FOOD LOVER"), "FOODLOVERMARKET",
               pd.np.where(temp.str.contains("DISCHEM"), "DISCHEM",
                pd.np.where(temp.str.contains("DIS-CHEM"), "DISCHEM",           
                pd.np.where(temp.str.contains("OK FOODS"), "OKFOODS",
                pd.np.where(temp.str.contains("DISCHEM"), "DISCHEM",
                pd.np.where(temp.str.contains("FASHION EXPRESS"), "FASHIONEXPRESS",
                pd.np.where(temp.str.contains("MTC"), "MTC",
                pd.np.where(temp.str.contains("TELECOM"), "TELECOM",
                pd.np.where(temp.str.contains("KFC"), "KFC",
                pd.np.where(temp.str.contains("ACKERMANS"), "ACKERMANS",
                pd.np.where(temp.str.contains("SHOPRITE"), "SHOPRITE",
                pd.np.where(temp.str.contains("USAVE"), "SHOPRITE",            
                pd.np.where(temp.str.contains("S/STATION"), "SERVICESTATION",
                pd.np.where(temp.str.contains("SERVICE STATION"), "SERVICESTATION",
                pd.np.where(temp.str.contains("SOULSTICE DAY SPA"), "SOULSTICESPA",
                pd.np.where(temp.str.contains("CLICKS" ), "CLICKS",
                pd.np.where(temp.str.contains("JET "), "JET",
                pd.np.where(temp.str.contains("PEP "), "PEP",           
               pd.np.where(temp.str.contains("WOERMANN"), "WOERMANN", "OTHER"))))))))))))))))))))))

我是否无法创建一个包含所有商户的列表,然后在每一行中循环一次,以确定商户名称是否出现在该行中,如果是,则输出商户名称,如果否,则将交易分类为其他?你知道吗

以下是数据示例:

Transactions Sample


Tags: 名称np交易where事务temppdcontains
2条回答

试试这个

list_names = ['PNP','CHECKERS'..]
mapping = ['PICKNPAY', 'CHECKERS' ..]
for i,item in enumerate(list_names):
    trax.loc[trax['Activity_2'].str.contains(item), 'Activity_2'] = mapping[i] 

#replaces inplace

我相信您可以创建一个包含名称的列表,并使用for循环将它们循环到其中的每一个。你知道吗

lst = ['PNP', 'JET'...]
for i in lst:
if df['Name'].str.contains[i]:     #same as your temp
    df['Short_Name'] = i

如果要为每行指定不同的名称,可以创建另一个包含“Short\u name”(“PNP”)和其他名称(“PICKNPAY”)的列表,并合并这两个数据帧。你知道吗

相关问题 更多 >