1

I am using pandas library in python for the following:

i currently have a dataset that looks like this :

ID       START      END
x         450       600
y         100       500
.          .         .

Here start and end represent footmarks, ie a vessel was parked here from point x to y.

What i want to do is create binary columns 1, 2 3... till the max value in 'END' and have it show a 1 if that column number is within the range of 'START' to 'END'

what i want to accomplish here is finding the total number of times all the footmarks have been used. Right now my approach is to have the binary columns and sum them up to get the total count.

I am also open to better methods of solving my problem.

edit_1:

What i want -

ID       START      END    1   2   3  ...... 450 ..... 500 .... 600   601  
x         450       600    0   0   0          1         1        1     0
y         100       500    0   0   0          1         1        0     0
.          .         .
6
  • what did you try so far ? Commented Aug 22, 2017 at 19:10
  • nothing so far, i looked up the get_dummies function but cant seem to figure out how to do that for a range of numbers. Commented Aug 22, 2017 at 19:12
  • @AasheetKumar, can you post your desired data set? Commented Aug 22, 2017 at 19:12
  • @MaxU will edit that in right now Commented Aug 22, 2017 at 19:14
  • @AasheetKumar, could you also explain why do you need such an unusual (not very efficient) structure? Commented Aug 22, 2017 at 19:36

1 Answer 1

1

If I understand correctly, you want to loop through values 1 to max('END') and produce True/False values based on range START to END. I think you want something like:

for i in range(1,df['END'].max()+1):
    df[i] = ((i >= df['START']) & (i <= df['END'])).astype(int)

However, if you only want to figure out the number of steps where all rows "overlap", you can just do:

max((df['END'].min() - df['START'].max()), 0)

Since the vessels will only overlap when everything is >= 'START' but <= 'END'

Sign up to request clarification or add additional context in comments.

1 Comment

the first part worked for me, thank you. The second code is not exactly what i am looking for, i have just added a comment to the question, maybe that helps explain my needs better. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.