1

I have a Dataframe with two columns and several thousand rows. First Column is called number and has no nan value. Second column is called minutes and has sometimes a value of random 0. I like to iterate through the whole Dataframe adding all the values under number where minutes is 0 to the next row where minutes is > 0.

df = pd.DataFrame({'number' : [25,35,21,12,7,45,50,23],
                       'minutes' : [0,0,50,75,0,0,0,80]}, 
                      index=['2018-02-14','2018-02-15','2018-02-16','2018-02-17','2018-02-18','2018-02-19','2018-02-20', '2018-02-21'])



    number  minutes
2018-02-14  25  0
2018-02-15  35  0
2018-02-16  21  50
2018-02-17  12  75
2018-02-18  7   0
2018-02-19  45  0
2018-02-20  50  0
2018-02-21  23  80

Desired Outcome would be:

    number  minutes
2018-02-14  0       0
2018-02-15  0   0
2018-02-16  81  50
2018-02-17  12  75
2018-02-18  0   0
2018-02-19  0   0
2018-02-20  0   0
2018-02-21  125     80

I tried to go over the df like that:

for i in range(0, len(df['minutes'])-1):
    if df['minutes'][i] == 0:
        value_one = df['number'][i]
        value_two = df['number'][i+1]
        new_value = value_one + value_two
        df['number'][i+1] = new_value
        df['number'][i] = 0

It works but it takes really long and I get a SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

Do you have ideas for improvement for me?

Thanks a lot

rudolf
  • 11
  • 1
  • The SettingWithCopyWarning is not a real problem: https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas, the speed is because oyu going over a for loop – PV8 Jun 12 '19 at 08:03
  • What would be the best alternative for the for loop? Maybe I can get rid of the warning as well. - thank you – rudolf Jun 12 '19 at 09:00

0 Answers0