DataFrame.append(other, ignore_index=False,
verify_integrity=False, sort=None)
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]}
df = pd.DataFrame(data=my_dict) # main dataframe
my_list=['New 1',7,66,56] # list with data
df_new=pd.DataFrame([my_list],columns=['NAME','ID','MATH','ENGLISH'])
df=df.append(df_new)
print(df)
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
0 New 1 7 66 56
The new row has retained its index as 0. df_new=pd.DataFrame([['New 1',7,66,56],['New 2',8,63,59],
['New 3',9,69,69]],
columns=['NAME','ID','MATH','ENGLISH'])
df=df.append(df_new)
print(df)
df=df.append(df_new,ignore_index=True)
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 New 1 7 66 56
4 New 2 8 63 59
5 New 3 9 69 69
6 New 1 7 66 56
7 New 2 8 63 59
8 New 3 9 69 69
my_dict={'NAME':'New 5','ID':10,'MATH':78,'ENGLISH':80}
df=df.append(my_dict,ignore_index=True)
print(df)
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 New 5 10 78 80
my_list=['new 6',10,80,88]
df.loc[len(df)]=my_list
print(df)
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]}
df = pd.DataFrame(data=my_dict) # main dataframe
#conditional adding#
my_list=[['new 4',4,80,88],['new 5',5,81,84],['new 6',6,76,78]]
for i in my_list:
if(i[2]+i[3]>=160):
df.loc[len(df)]=i
print(df)
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 new 4 4 80 88
4 new 5 5 81 84
Here we are checking all rows for the condition and then adding the matching row to the DataFrame. We can pre-check the conditions by applying filters and then add rows to main DataFrame
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]}
df = pd.DataFrame(data=my_dict) # main dataframe
#new rows to add #
my_list=[['new 4',4,80,88],['new 5',5,81,84],['new 6',6,76,78]]
df_new=pd.DataFrame(my_list,
columns=['NAME','ID','MATH','ENGLISH'])
#filter who got more than equal to 160 in both math and English #
df_new=df_new[(df_new[['MATH','ENGLISH']].sum(axis=1))>=160]
df=df.append(df_new)
print(df)
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
0 new 4 4 80 88
1 new 5 5 81 84
You can add not (~
) condition to this
df_new=df_new[~(df_new[['MATH','ENGLISH']].sum(axis=1)>=160)]
Output is here
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
2 new 6 6 76 78
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]}
df = pd.DataFrame(data=my_dict) # main dataframe
df['SCIENCE']=[34,35,36]
print(df)
Output
NAME ID MATH ENGLISH SCIENCE
0 Ravi 1 30 20 34
1 Raju 2 40 30 35
2 Alex 3 50 40 36
Pandas
Pandas DataFrame sort_values
groupby
cut
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.