【问题标题】:Is there a python function I can use to group retail banking transactions?是否有可用于对零售银行交易进行分组的 python 函数?
【发布时间】:2019-08-19 20:19:59
【问题描述】:

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"))))))))))))))))))))))

有没有办法我可以创建一个包含所有商家的列表,然后提出一个循环,该循环在每一行中循环以确定商家名称是否出现在该行中,如果是,则输出商家名称,如果否将交易归类为其他交易?

以下是数据示例:

【问题讨论】:

  • 什么是商家名称?你已经有商家名单了吗?您的典型输入和您想要的函数的预期行为是什么?
  • 不,我没有商家列表,所以我需要创建自己的列表
  • 这是numpy.select。不要链接np.where

标签: python python-3.x pandas numpy


【解决方案1】:

试试这个,

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

【讨论】:

    【解决方案2】:

    我相信您可以创建一个包含名称的列表并使用 for 循环循环到每个列表中。

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

    如果您想为每一行指定不同的名称,您可以创建一个包含“Short_Name”(“PNP”)和其他名称(“PICKNPAY”)的另一个列表,并将两个数据框合并。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 2019-05-29
      • 2019-01-02
      相关资源
      最近更新 更多