简体   繁体   English

用于pos数据的python中的数据清理for循环

[英]Data cleaning for-loop in python for pos data

I have a pos data of message shop. 我有邮件商店的POS数据。 The Data is as shown in attached picture. 数据如附图所示。 在此处输入图片说明

##read data from csv
data = pd.read_csv('test1.csv')
#make a kist for each column
sales_id = list(data['sales_id'])
shop_number = list(data['shop_number'])
sales = list(data['sales'])
cashier_no = list(data['cashier_no'])
messager_no = list(data['messager_no'])
type_of_sale = list(data['type_of_sale'])
costomer_ID = list(data['costomer_ID'])
type_of_sale = list(data['type_of_sale'])
date = list(data['date'])
time = list(data['time'])

I want make a new list showing that the data of purchase should be deleted. 我要创建一个新列表,显示应该删除购买数据。 like this: 像这样:

data_to_clean= [0,1,0,1,0,0,1,0,1]

To do it I want to make a for loop 要做到这一点,我想做一个for循环

for i in range(len(type_of_sale)):
    data_to_clean=[]
    if type_of_sale[i] == "purchase":
        data_to_clean = data_to_clean.append(0)
    elif type_of_sale[i] == "return":
        data_to_clean = data_to_clean.append(1)
        ## I want to write a code so I can delete purchasse data too 
        #with conditions if it has the same shop_number,messager_no,costomer_ID and -price

    return list(data_to_clean)

There is two main problem in this code. 这段代码有两个主要问题。 One it doesn't move. 一不动。 2nd I don't know how to check shop_number , messager_no and costomer_ID to put 1 or 0 in my data_to_clean list. 第二,我不知道如何检查shop_numbermessager_nocostomer_ID把1或0在我data_to_clean名单。 sometimes I have to check for the data above like sales_id(1628060) and sometimes its below like sales_id(1599414) Knowing that the cashier may differ. 有时我要检查的数据等以上sales_id(1628060)有时它的下面等sales_id(1599414)即使知道出纳员可以不同。 but the constomer_Id should be the same always. 但constomer_Id应该始终相同。

The question is how to write a the code so I can create a list or dataframe with 0 and 1 to show which data should be deleted. 问题是如何编写代码,这样我可以创建一个列表或带有0和1的数据框,以显示应删除的数据。

When you want to compare data with string in Python, you should put this string in qoutes: 当您想将数据与Python中的字符串进行比较时,应将此string放在qoutes中:

for i in range(len(type_of_sale)):
        data_to_clean=[]
        if type_of_sale[i] == "purchase": # here
            data_to_clean = data_to_clean.append(0)
        elif type_of_sale[i] == "return": # and here
            data_to_clean = data_to_clean.append(1)

check pandas doc . 检查熊猫文档 Getting the items which are a return order can be as simple as 获取作为退货订单的物品可以很简单

returns = data.loc[data['type_of_sale'] == 'return']

If you want the sales of cashier 90 如果要销售收银员90

data.loc[(data['type_of_sale'] == 'purchase') & (data['cashier_no'] == 90)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM