Python Pandas - Add Rows to DataFrame

preview_player
Показать описание
In this Python Pandas tutorial, we will go over several ways to add rows to a DataFrame.
Рекомендации по теме
Комментарии
Автор

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
'''

RyanNoonan
Автор

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

diamondyt
Автор

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

tigerWarrior