简体   繁体   English

迭代 pandas dataframe 并改变值

[英]Iterating pandas dataframe and changing values

I'm looking to predict the number of customers in a restaurant at a certain time.我想预测某个时间餐厅的顾客数量。 My data preprocessing is almost finished - I have acquired the arrival dates of each customer.我的数据预处理即将完成——我已经获得了每个客户的到达日期。 Those are presented in acchour .这些在acchour中呈现。 weekday means the day of the week, 0 being Monday and 6 Sunday. weekday表示星期几,0 表示星期一,6 表示星期日。 Now, I'd like to calculate the number of customers at that certain time in the restaurant.现在,我想计算餐厅在特定时间的顾客数量。 I figured I have to loop through the dataframe in reverse and keep adding the arrived customers to the customer count at a certain time, while simultaneously keeping track of when previous customers are leaving.我想我必须反向循环 dataframe 并在特定时间继续将到达的客户添加到客户计数中,同时跟踪以前的客户何时离开。 As there is no data on this, we will simply assume every guest stays for an hour.由于没有这方面的数据,我们将简单地假设每位客人停留一个小时。

My sketch looks like this:我的草图看起来像这样:

exp = [] #Keep track of the expiring customers

for row in reversed(df['customers']): #start from the earliest time
    if row != 1: #skip the 1st row
        res = len(exp) + 1 #amount of customers
        for i in range(len(exp) - 1, -1, -1): #loop exp sensibly while deleting
            if df['acchour'] > exp[i]+1: #if the current time is more than an hour more than the customer's arrival time
                res -= 1
                del exp[i]
        exp.append(df['acchour'])
        row = res

However, I can see that df['acchour'] is not a sensible expression and was wondering how to reference the different column on the same row properly.但是,我可以看到df['acchour']不是一个明智的表达,并且想知道如何正确引用同一行上的不同列。 Altogether, if someone can come up with a more convenient way to solve the problem, I'd hugely appreciate it!总之,如果有人能想出更方便的方法来解决问题,我将不胜感激!

我的数据框如下所示:

So you can get the total customers visiting at a specific time like so:所以你可以得到在特定时间访问的总客户,如下所示:

df.groupby(['weekday','time', 'hour'], as_index=False).sum()

Then maybe you can calculate the difference between each time window you want?那么也许你可以计算出你想要的每次window之间的差异?

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

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