【发布时间】: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