other: DataFrame, dictionary,list, series to add ignore_index : boolean, default False, If True do not use index labels verify_integrity : boolean Default is false, value error if index has duplicates sort : boolean , Default is None , Sort columns if the columns of self and other are not aligned.
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.
We can have list with more number of rows.
While using append() we can specify not to use the index of added list or DataFrame.
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
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
Adding list to DataFrame using loc
We will create a list and then add the list ( my_list ) at the end of the DataFrame. the Function len() will return the number of rows, the index position of last row is 1 less than the total number of rows. So the new row will be added at the end of the DataFrame. This way we can add data row without headers or column names.
We will use loc to add the list.
Here row will be added if the condition is satisfied. Here the sum of two subject mark must be equal to or more than 160 to get added to the 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
#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
Using Filter
We can create DataFrame with the required condition and then append the same to main DataFrame. Here we are creating the new DataFrame using condition that sum of ( marks ) in MATH and ENGLISH is equal to or more than 160. Here out of three new records only two qualifies. These two records we will append to our main DataFrame 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
#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