'group and modify rows by index python pandas
I have a question on pandas-python: Say I have a column with 1000+ elements (integers), and its name is 'a' like
'a' = [2,2,2,2,2,2,2,2,2,2,2,2,..............]
I want to modify the elements by row index(every third number increase by 1 till the end of the column), and deleting the name. My final (and desired) output wold be:
'b' = [2,2,2,3,3,3,4,4,4,5,5,5..................]
Any hint?
Solution 1:[1]
a = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
c = []
for i in range(0, len(a), 3):
b = a[i : i + 3]
for j in range(len(b)):
b[j] = b[j] + (i // 3)
c.extend(b)
print(c)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Parvesh Kumar |
