Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the below dataframe:

   id     a       b

    1     1       5

    2    -5       0

    3     0       5

    4     5       6

and I  am trying to duplicate the first and last row of the dataframe:

   id     a       b

    1     1       5

    1     1       5

    2    -5       0

    3     0       5

    4     5       6

    4     5       6

I have many data frames in the list so I am trying to using this code:

for df in list:

    pd.concat([df.head(1), df], axis=1)

    pd.concat([df.tail(1), df], axis=1)

It is not working, can anyone help me solve it?

1 Answer

0 votes
by (36.8k points)

Check the below code:

for i, df in enumerate(list):

    list[i] = pd.concat([df.head(1), df ,df.tail(1)], axis=1)

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...