1# append row to dataframe without index
2
3a_row = pd.Series([1, 2])
4df = pd.DataFrame([[3, 4], [5, 6]])
5
6row_df = pd.DataFrame([a_row])
7df = pd.concat([row_df, df], ignore_index=True)
8
9print(df)
10# OUTPUT
11# 0 1
12# 0 1 2
13# 1 3 4
14# 2 5 6
15
16# append row to dataframe with index
17
18a_row = pd.Series([1, 2])
19df = pd.DataFrame([[3, 4], [5, 6]], index = ["row1", "row2"])
20
21row_df = pd.DataFrame([a_row], index = ["row3"])
22df = pd.concat([row_df, df])
23
24print(df)
25# OUTPUT
26# 0 1
27# row3 1 2
28# row1 3 4
29# row2 5 6
1import pandas as pd
2
3data = {'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
4 'physics': [68, 74, 77, 78],
5 'chemistry': [84, 56, 73, 69],
6 'algebra': [78, 88, 82, 87]}
7
8
9#create dataframe
10df_marks = pd.DataFrame(data)
11print('Original DataFrame\n------------------')
12print(df_marks)
13
14new_row = {'name':'Geo', 'physics':87, 'chemistry':92, 'algebra':97}
15#append row to the dataframe
16df_marks = df_marks.append(new_row, ignore_index=True)
17
18print('\n\nNew row added to DataFrame\n--------------------------')
19print(df_marks)
1# Add a new row at index k with values provided in list
2dfObj.loc['k'] = ['Smriti', 26, 'Bangalore', 'India']
3
1 df.loc[-1] = [2, 3, 4] # adding a row
2 df.index = df.index + 1 # shifting index
3 df = df.sort_index() # sorting by index
1>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
2>>> df
3 A B
40 1 2
51 3 4
6>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
7>>> df.append(df2)
8 A B
90 1 2
101 3 4
110 5 6
121 7 8