djj1994
djj1994

Reputation: 55

How to create an array/dataframe populated by 1 or 0 based on whether time is within a range?

Basically, I have a dataframe which has 2 columns, both of which are hours:

    0    1
 +-----+----+    
0|  11 | 12 |
 +-----+----+
1|   3 | 4  |
 +-----+----+
2|  11 | 12 |
 +-----+----+
3|   6 |  7 |
 +-----+----+
4|  16 | 16 |

     etc...

This has a few thousand rows. I want to make another dataframe which has column headers '1' to '24' (based on the hours of a 24 hour period) and for each row of the dataframe above displays 1 if the hour time is within that range (inclusive) and 0 if it is outside.

So for example the second row of the above dataframe would be something like:

1   2   3   4   5   6   7   8 ......24
0   0   1   1   0   0   0   0 ......0

And I want to do the same for each row of the first dataframe and append to the new 24 hour data frame.

Hopefully this makes sense and someone can help! Happy to walk through further if it doesn't make sense! Also am new to posting on here so not sure exactly how to get the data to paste over in a sensible way.

Upvotes: 1

Views: 52

Answers (3)

DTT
DTT

Reputation: 39

This is another possible solution:

import pandas as pd
df=pd.DataFrame([[11, 12],[3,   4],[11,  12],[6,  7],[16,  16]], columns=[0,1])
print("df= \n ",df)

dg=pd.DataFrame(columns=np.arange(1,26))
for i in df.index:
    for j in [0,1]:
        dg.loc[i,df.iloc[i,j]]=1
print("dg= \n", dg.fillna(0))

df= 
      0   1
0  11  12
1   3   4
2  11  12
3   6   7
4  16  16
dg= 
    1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25
0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0
1   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
2   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0
3   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0

Upvotes: 0

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

You can compare and multiply the values by creating a dataframe i.e

temp = pd.DataFrame([np.arange(1,25)],index = df.index,)

begin = (temp.values>=df['0'].values[:,None]).astype(int)
end = (temp.values<=df['1'].values[:,None]).astype(int)


pd.DataFrame(begin*end,columns=np.arange(1,25))

    1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24
0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0
1   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
2   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0
3   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0

Upvotes: 1

jezrael
jezrael

Reputation: 863301

Use:

df = (pd.DataFrame([dict.fromkeys(range(a, b + 1), 1) for a, b in zip(df[0], df[1])])
        .reindex(columns=range(1, 25), fill_value=0)
        .fillna(0)
        .astype(int))

print (df)
   1   2   3   4   5   6   7   8   9  ...  16  17  18  19  20  21  22  23  24
0   0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0
1   0   0   1   1   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0
2   0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0
3   0   0   0   0   0   1   1   0   0 ...   0   0   0   0   0   0   0   0   0
4   0   0   0   0   0   0   0   0   0 ...   1   0   0   0   0   0   0   0   0

[5 rows x 24 columns]

Upvotes: 1

Related Questions