pandas append a row to a dataframe code example
Example 1: pandas append index ignore
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
>>> df.append(df2, ignore_index=True)
A B
0 1 2
1 3 4
2 5 6
3 7 8
Example 2: how to add a row to a dataframe in python
import pandas as pd
data = {'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
'physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]}
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)
new_row = {'name':'Geo', 'physics':87, 'chemistry':92, 'algebra':97}
df_marks = df_marks.append(new_row, ignore_index=True)
print('\n\nNew row added to DataFrame\n--------------------------')
print(df_marks)
Example 3: add new row to dataframe pandas
dfObj.loc['k'] = ['Smriti', 26, 'Bangalore', 'India']