0

in my df i want to add row when its meets specific condition,df has thousands of rows.In id column when a new value of id is started i want to add new row copying same id value. df is as as below

id                        name                              value
0000                    length                                46
0000                    status                              completed
0000                    segment                              21
1111                     tp                                 0.1
1111                     x                                  56
2222                     point                              23.01
2222                     x                                  50
2222                     y                                  40
expected output is

     id                        name                              value
    0000                       type                                description #new row
    0000                    length                                46
    0000                    status                              completed
    0000                    segment                               21
    1111                       type                               description  #new row
    1111                     tp                                   0.1
    1111                     x                                    56
    2222                     type                                description  #new row
    2222                     point                                23.01
    2222                     x                                    50
    2222                     y                                    40
i know about loc[] or iloc[] but i can not use them as i dont know the exact index or position.I am new to pandas. Is there a way to do it? any help will be ppreciated

2
  • whats the condition? Commented Dec 31, 2019 at 12:32
  • condition is that when new id is started means 0000 switch to 1111 then before switching to 1111 add new row where id is 1111 and name is type Commented Dec 31, 2019 at 12:37

1 Answer 1

2
new = df.groupby('id', as_index=False).first().assign(name='type', value='description  #new row')
df = pd.concat([new, df]).sort_values('id')

print(df)

Prints:

     id     name                  value
0  0000     type  description  #new row
0  0000   length                     46
1  0000   status              completed
2  0000  segment                     21
1  1111     type  description  #new row
3  1111       tp                    0.1
4  1111        x                     56
2  2222     type  description  #new row
5  2222    point                  23.01
6  2222        x                     50
7  2222        y                     40

Note: this assumes that column id is sorted before.

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

3 Comments

it work fine for first row but then it add new row at end of id and some time it add same id row twice. how to fix it?
@user19 Before running this code, try to sort column id, e.g df = df.sort_values('id').reset_index()
I already tried it I mean id column is sorted but still same problem

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.