2

I got a problem dealing with dataframe. I want to insert rows in DF and modify its related values:

for example I have

time  value
0  10:00      2
1  11:00      4
2  12:00      6
3  13:00      8

and the ideal result should be:

time  value
0  10:00      1
1  11:00      2
0  10:00      1
1  11:00      2
2  12:00      6
3  13:00      8

Which duplicate first two rows and then divide its value evenly.

The code can only append rows but how to modify its values in a faster way? df[0:2].append(df[0:2]).append(df[2:])

1 Answer 1

3
first_two = df[: 2]
first_two.value = first_two.value / 2

df = pd.concat([first_two, first_two, df[2:]])

Edit

as Paul H points out below, it's better touse iloc for this stuff.

Sign up to request clarification or add additional context in comments.

1 Comment

Better to use the .iloc attribute here. IOW: first_two = df.iloc[:2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.