Creating a loop program to apply values to pandas df

HenryHub :

This question maybe super basic and apologize for that..

But I am trying to create a for loop that would enter a value of 1 or 0 into a pandas dataframe based on a condition.

import pandas as pd

def checkHour6(time):
    val = 0
    if  time == 6:
        val = 1 
    return val

def checkHour7(time):
    val = 0
    if  time == 7:
        val = 1 
    return val

def checkHour8(time):
    val = 0
    if  time == 8:
        val = 1 
    return val

def checkHour9(time):
    val = 0
    if  time == 9:
        val = 1 
    return val

def checkHour10(time):
    val = 0
    if  time == 10:
        val = 1 
    return val

This for loop that I am attempting will count from 0 to 23, and I am attempting to building pandas dataframe in the loop process that will enter a value of a 1 or 0 appropriately but I am missing something basic as the final df result is an empty dataframe.

Create empty df:

df = pd.DataFrame({'hour_6':[], 'hour_7':[], 'hour_8':[], 'hour_9':[], 'hour_10':[]})

For Loop:

hour = -1

for i in range(24):
    stuff = []
    hour = hour + 1
    stuff.append(checkHour6(hour))
    stuff.append(checkHour7(hour))
    stuff.append(checkHour8(hour))
    stuff.append(checkHour9(hour))
    stuff.append(checkHour10(hour))
    df.append(stuff)
Jaroslav Bezděk :

I would suggest the following:

  • use only one checkHour() function with a parameter for hour,
  • according to pandas.DataFrame.append() documentation, other parameter has to be DataFrame or Series/dict-like object, or list of these, so list cannot be used,
  • if you want to make a data frame by appending new rows to the existing one, you have to assign it.

The code can look like this:

def checkHour(time, hour):
    val = 0
    if time == hour:
        val = 1 
    return val

df = pd.DataFrame({'hour_6':[], 'hour_7':[], 'hour_8':[], 'hour_9':[], 'hour_10':[]})

hour = -1

for i in range(24):
    stuff = {}
    hour = hour + 1
    stuff['hour_6'] = checkHour(hour, 6)
    stuff['hour_7'] = checkHour(hour, 7)
    stuff['hour_8'] = checkHour(hour, 8)
    stuff['hour_9'] = checkHour(hour, 9)
    stuff['hour_10'] = checkHour(hour, 10)
    df = df.append(stuff, ignore_index=True)

The result is following:

>>> print(df)
    hour_6  hour_7  hour_8  hour_9  hour_10
0      0.0     0.0     0.0     0.0      0.0
1      0.0     0.0     0.0     0.0      0.0
2      0.0     0.0     0.0     0.0      0.0
3      0.0     0.0     0.0     0.0      0.0
4      0.0     0.0     0.0     0.0      0.0
5      0.0     0.0     0.0     0.0      0.0
6      1.0     0.0     0.0     0.0      0.0
7      0.0     1.0     0.0     0.0      0.0
8      0.0     0.0     1.0     0.0      0.0
9      0.0     0.0     0.0     1.0      0.0
10     0.0     0.0     0.0     0.0      1.0
11     0.0     0.0     0.0     0.0      0.0
12     0.0     0.0     0.0     0.0      0.0
13     0.0     0.0     0.0     0.0      0.0
14     0.0     0.0     0.0     0.0      0.0
15     0.0     0.0     0.0     0.0      0.0
16     0.0     0.0     0.0     0.0      0.0
17     0.0     0.0     0.0     0.0      0.0
18     0.0     0.0     0.0     0.0      0.0
19     0.0     0.0     0.0     0.0      0.0
20     0.0     0.0     0.0     0.0      0.0
21     0.0     0.0     0.0     0.0      0.0
22     0.0     0.0     0.0     0.0      0.0
23     0.0     0.0     0.0     0.0      0.0

EDIT:

As @Parfait mentioned, it is not good to use pandas.DataFrame.append() in for loop, because it leads to quadratic copying. To avoid that, you can make a list of dictionaries (future data frame rows) and after that call pd.DataFrame() to make a data frame out of it. The code looks like this:

def checkHour(time, hour):
    val = 0
    if time == hour:
        val = 1 
    return val

data = []
hour = -1

for i in range(24):
    stuff = {}
    hour = hour + 1
    stuff['hour_6'] = checkHour(hour, 6)
    stuff['hour_7'] = checkHour(hour, 7)
    stuff['hour_8'] = checkHour(hour, 8)
    stuff['hour_9'] = checkHour(hour, 9)
    stuff['hour_10'] = checkHour(hour, 10)
    data.append(stuff)

df = pd.DataFrame(data)

And the result is following:

>>> print(df)
    hour_6  hour_7  hour_8  hour_9  hour_10
0        0       0       0       0        0
1        0       0       0       0        0
2        0       0       0       0        0
3        0       0       0       0        0
4        0       0       0       0        0
5        0       0       0       0        0
6        1       0       0       0        0
7        0       1       0       0        0
8        0       0       1       0        0
9        0       0       0       1        0
10       0       0       0       0        1
11       0       0       0       0        0
12       0       0       0       0        0
13       0       0       0       0        0
14       0       0       0       0        0
15       0       0       0       0        0
16       0       0       0       0        0
17       0       0       0       0        0
18       0       0       0       0        0
19       0       0       0       0        0
20       0       0       0       0        0
21       0       0       0       0        0
22       0       0       0       0        0
23       0       0       0       0        0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350960&siteId=1