I
I
Ivan Melnikov2021-11-28 20:04:51
Python
Ivan Melnikov, 2021-11-28 20:04:51

How to loop through the first 10 rows in a DataFrame and increase the value in them by 1?

Is it possible to change the value of fields in a loop in Pandas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-11-28
@dmshar

"Is it possible in Pandas to change the value of fields in a loop?" - can. But in the question that stands in the title - this is not necessary.
I'm too lazy to fill a large DataFrame, so here's an abbreviated example. There are 10 rows in the dataframe, we change the first column in the first five rows.

print (df)
df.A.loc[:4]=df.A.loc[:4]+1
print (df)

Dataframe before manipulation:
.   A  B
0  1  2
1  2  4
2  3  6
3  4  8
4  5  0
5  1  2
6  2  4
7  3  6
8  4  8
9  5  0

Dataframe after manipulation:
.   A  B
0  2  2
1  3  4
2  4  6
3  5  8
4  6  0
5  1  2
6  2  4
7  3  6
8  4  8
9  5  0

I hope you can adjust to your data yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question