Python Pandas – Add Rows to DataFrame




[ad_1]

In this Python Pandas tutorial, we will go over several ways to add rows to a DataFrame.

Source


[ad_2]

Comment List

  • Ryan Noonan
    December 17, 2020

    I was experimenting with adding rows in other different ways and here are a couple more examples that appear to work. Also if you have a letter index you can use that with loc (as opposed to numbers).

    import pandas

    df = pandas.DataFrame({'A': [1,2,3,4,5],
    'B': [10,20,30,40,50],
    'C': [100,200,300,400,500],
    'D': [1000,2000,3000,4000,5000],
    'E': [10000,20000,30000,40000,50000]})

    # add rows together
    df.loc[5] = df.loc[0] + df.loc[1]

    # add total sum row
    df.loc[6] = df.loc[:].sum()

    print(df)

    # prints
    '''
    A B C D E
    0 1 10 100 1000 10000
    1 2 20 200 2000 20000
    2 3 30 300 3000 30000
    3 4 40 400 4000 40000
    4 5 50 500 5000 50000
    5 3 30 300 3000 30000
    6 18 180 1800 18000 180000
    '''

  • Ryan Noonan
    December 17, 2020

    ily <3 i liked the video, you helped me with my school project 🙂 thanks again <3

  • Ryan Noonan
    December 17, 2020

    thank uuuuu a lot

  • Ryan Noonan
    December 17, 2020

    I need speed tests.

  • Ryan Noonan
    December 17, 2020

    How do you add empty row on top of data frame.?

Write a comment