How to add one row to Pandas DataFrame

  1. .loc[index] method to add the row to Pandas dataframe with lists
  2. append dictionary as the row to add it to Pandas dataframe
  3. Dataframe .append method to add a row

Pandas is designed to load a fully populated dataframe. We can add row one by one to pandas.dafaframe. This can be done by using various approaches like .loc, dictionaries, pandas.concat() or DataFrame.append().

.loc[index] method to add the row to Pandas dataframe with lists

loc[index] will take the new list as a new row and add it to give index of pandas.dataframe.

Consider the following code:

# python 3.x
import pandas as pd
# List of Tuples
fruit_list = [ ('Orange', 34, 'Yes' )]
#Create a DataFrame object
df = pd.DataFrame(fruit_list, columns = ['Name' , 'Price', 'Stock'])
#Add new ROW
df.loc[1]=[ 'Mango', 4, 'No' ]
df.loc[2]=[ 'Apple', 14, 'Yes' ]
print(df)

Output:

     Name  Price Stock
0  Orange     34   Yes
1   Mango      4    No
2   Apple     14   Yes

append dictionary as the row to add it to Pandas dataframe

append() can take the value of a dictionary of key-value pair directly as a row and add it to pandas dataframe.

Consider the following code:

# python 3.x
import pandas as pd
# List of Tuples
fruit_list = [ ('Orange', 34, 'Yes' )]
#Create a DataFrame object
df = pd.DataFrame(fruit_list, columns = ['Name' , 'Price', 'Stock'])
#Add new ROW
df=df.append({'Name' : 'Apple' , 'Price' : 23, 'Stock' : 'No'} , ignore_index=True)
df=df.append({'Name' : 'Mango' , 'Price' : 13, 'Stock' : 'Yes'} , ignore_index=True)
print(df)

Output:

     Name  Price Stock
0  Orange     34   Yes
1   Apple     23    No
2   Mango     13   Yes

Dataframe .append method to add a row

.append can be used to append rows of other dataframe to the end of the original dataframe, and return a new dataframe. Columns from new dataframe which are not in original datafarme also added to existing dataframe and new cells value are filled with NaN.

Consider the following code:

# python 3.x
import pandas as pd
# List of Tuples
fruit_list = [ ('Orange', 34, 'Yes' )]
#Create a DataFrame object
df = pd.DataFrame(fruit_list, columns = ['Name' , 'Price', 'Stock'])
print("Original DataFrame:")
print(df)
print('.............................')
print('.............................')
new_fruit_list = [ ('Apple', 34, 'Yes','small' )]
dfNew=pd.DataFrame(new_fruit_list, columns = ['Name' , 'Price', 'Stock','Type'])
print("Newly Created DataFrame:")
print(dfNew)
print('.............................')
print('.............................')
#append one dataframe to othher
df=df.append(dfNew,ignore_index=True)
print("Copying DataFrame to orignal...")
print(df)

ignore_index=True will ignore the index of new dataframe and assign them new index from original dataframe.

Output:

Original DataFrame:
     Name  Price Stock
0  Orange     34   Yes
.............................
.............................
Newly Created DataFrame:
    Name  Price Stock   Type
0  Apple     34   Yes  small
.............................
.............................
Copying  DataFrame to original..:
     Name  Price Stock   Type
0  Orange     34   Yes    NaN
1   Apple     34   Yes  small

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to add header row to a pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to delete a row based on column value in Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • comments powered by Disqus