Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update and append new rows based on index value python

I have two dataframes DF1, DF2 which have the same type of data and share some index values but not all

        DF1    
 index, a, b, c
[ abc   1, 3, 6 ]
[ acb   2, 4, 5 ]
[ cab   6, 5, 2 ]
[ bac   3, 6, 2 ]
[ bca   6, 8, 3 ]

        DF2
 index, a, b, d
[ abc   4, 7, 3 ]
[ kde   2, 5, 8 ]
[ lat   7, 2, 6 ]
[ bac   0, 4, 4 ]
[ bca   3, 6, 8 ]

as a result I want to achieve the following

1.) Add column D to DF1 based on the index match

2.) Add index and rows from DF2which are not present in DF1

        RESULT   
 index, a, b, c, d
[ abc   1, 3, 6, 3 ]
[ acb   2, 4, 5, - ]
[ cab   6, 5, 2, - ]
[ bac   3, 6, 2, 4 ]
[ bca   6, 8, 3, 8 ]
[ kde   2, 5, -, 8 ]
[ lat   7, 2, -, 6 ]
like image 505
Aran Freel Avatar asked Apr 06 '17 16:04

Aran Freel


People also ask

How do you update data in a DataFrame in Python?

Pandas DataFrame update() Method The update() method updates a DataFrame with elements from another similar object (like another DataFrame). Note: this method does NOT return a new DataFrame. The updating is done to the original DataFrame.

How to append a new row in a Dataframe in Python?

To handle tabular data in python, we normally use dataframes. In this article, we will discuss how we can append a new row in a dataframe. Append a New Row in a Dataframe Using the loc[] Attribute If we have a row given in the form of a list, we can use the loc[] property defined in the pandas module to add the row to the dataframe.

How to append an appended index to a list in Python?

In case of collection of indices, all of them gets appended to the original index in the same order as they are passed to the Index.append() function. The function returns an appended index. Syntax: Index.append(other) Parameters : other : Index or list/tuple of indices. Returns : appended : bool or array_like (if axis is specified)

How to update the value of a row in a Dataframe?

How to update the value of a row in a Python Dataframe? 1. Using Python at () method to update the value of a row. Python at () method enables us to update the value of one row... 2. Python loc () function to change the value of a row/column. Python loc () method can also be used to update the ...

How do I change the value of a row in Python?

Using Python at () method to update the value of a row Python at () method enables us to update the value of one row at a time with respect to a column. In this example, we have provided the at () function with index 6 of the data frame and column ‘NAME’.


1 Answers

Let use combine_first:

Create data:

DF1 = pd.DataFrame({'a':[1,2,6,3,6],'b':[3,4,5,6,8],'c':[6,5,2,2,3]},index=['abc','acb','cab','bac','bca'])
DF2 = pd.DataFrame({'a':[4,2,7,0,3],'b':[7,5,2,4,6],'d':[3,8,6,4,8]},index=['abc','kde','lat','bac','bca'])

df_combo = DF1.combine_first(DF2)
print(df_combo)

       a    b    c    d
abc  1.0  3.0  6.0  3.0
acb  2.0  4.0  5.0  NaN
bac  3.0  6.0  2.0  4.0
bca  6.0  8.0  3.0  8.0
cab  6.0  5.0  2.0  NaN
kde  2.0  5.0  NaN  8.0
lat  7.0  2.0  NaN  6.0
like image 87
Scott Boston Avatar answered Sep 17 '22 22:09

Scott Boston