DataFrame.append()

We can add row, list , dictionary , series to any DataFrame by using append()
DataFrame.append(other, ignore_index=False, 
verify_integrity=False, sort=None)

Options

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.

Examples using options

Create one list and then add to 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 
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.
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)

ignore_index

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

Adding directory using append()

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

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.
my_list=['new 6',10,80,88]
df.loc[len(df)]=my_list
print(df)

Conditional adding of data

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

Adding a column

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
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer