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:
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
.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
.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]
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With