I have the following dataframe : On every 11th row i would like to insert -1. Tried a few suggestion but did not get what i expected.
Asked
Active
Viewed 38 times
-2
-
1Show what you tried as properly formatted code in the question. – Michael Butscher Jan 07 '21 at 00:11
-
1[It works just like Python list slicing](https://stackoverflow.com/a/31593712/8881141): `df.loc[::11, "column name"] -= 1` – Mr. T Jan 07 '21 at 00:13
1 Answers
0
Maybe you could use pandas.DataFrame.iloc
:
>>> import pandas as pd
>>> d = {'%UNWEIGHTED_ILI': [0.184361, 0.527357, 0.330384, 0.100392,
0.491726, 0.433408, 0.117853, 0.129628,
0.204399, 0.342965, 0.123456, 0.976543]}
>>> df = pd.DataFrame(data=d)
>>> df
%UNWEIGHTED_ILI
0 0.184361
1 0.527357
2 0.330384
3 0.100392
4 0.491726
5 0.433408
6 0.117853
7 0.129628
8 0.204399
9 0.342965
10 0.123456
11 0.976543
>>> df.iloc[::11, :] -= 1
>>> # df.iloc[11::11, :] -= 1 If you don't want -1 to be added to the first row
>>> df
%UNWEIGHTED_ILI
0 -0.815639
1 0.527357
2 0.330384
3 0.100392
4 0.491726
5 0.433408
6 0.117853
7 0.129628
8 0.204399
9 0.342965
10 0.123456
11 -0.023457

Sash Sinha
- 18,743
- 3
- 23
- 40
-
I would like to insert a -1 rather. Not add to the existing data. And also it has to be for every 11th row. Thanks for your answer. – CD_NS Jan 07 '21 at 00:25