Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making lists based on values in column

I have an interesting case. In column FID2 I have some values, based on each i'd like to create a list. The column Ncircles determines the list. For example:

  • If there's a value 0 in Ncircles, i'd like to create a list based on the value in FID2 in the same row as [i], where i is equal to FID2.
  • If there's a value 1 in Ncircles, i'd like to create a list based on the value in FID2 in the same row as [i-1, i, i +1], where i is equal to FID2.
  • If there's a value 3 in Ncircles, i'd like to create a list based on the value in FID2 in the same row as [i-3, i-2, i -1 i, i+1, i+2, i +3], where i is equal to FID2.

This is an example of df:

          FID2  Ncircles
0        50141         0
1        56188         1
2        75035         0
3        94937         3

The final lists can be written all in the same, one list. Do you have any suggestions how to do this?

An expected output would be a new list:

Newlist = [50141, 56187, 56188, 56188, 75035, 94934, 94935, 94936, 94937, 94938, 94939, 94940]
like image 788
energyMax Avatar asked Mar 04 '23 11:03

energyMax


1 Answers

Use range in list comprehension with flattening:

Newlist = [c for a, b in zip(df['FID2'], df['Ncircles']) for c in range(a-b, a+b+1)]
print (Newlist)
[50141, 56187, 56188, 56189, 75035, 94934, 94935, 94936, 94937, 94938, 94939, 94940]
like image 189
jezrael Avatar answered Mar 16 '23 01:03

jezrael