Nalin Samarasinghe
Nalin Samarasinghe

Reputation: 19

pandas use index to add new values in a new column

I have two data frames and I want to make a single data frame.

I si the index and V is the value that I am interested.

df1 is like

I V

A   4   
B   5       
C   8       
D   6      
F   2       

df2 is like

I V

A   8
C   6
D   9
E   4    
G   7

I want the output like

I V1 v2

A   4   8
B   5   -    
C   8   6    
D   6   9   
E   -   4    
F   2   -    
G   -   7

Is there a direct method in Pandas that can do this? or do I have to use a loop to iterate through the set of all indexes and enter value cell by cell?

as you can see df1 and df2 has few unique rows.

I am really sorry about the formatting of these tables.

I was not able to figure out how to format this yet.

EDIT: Yes I initially posted this with the wrong data for df1.

at the end I used merge.

Upvotes: 2

Views: 85

Answers (2)

Joachim
Joachim

Reputation: 3270

You don't even need to merge. Just construct a new DataFrame with df1 and df2 as columns.

index2 = 'abcdef'
index1 = 'abcdeg'
df1 = pd.DataFrame(index=list(index1), data=list(range(len(index1))))
df2 = pd.DataFrame(index=list(index2), data=list(range(len(index2))))
pd.DataFrame(data={'a': df1.iloc[:, 0], 'b': df2.iloc[:, 0]})

     a    b
a  0.0  0.0
b  1.0  1.0
c  2.0  2.0
d  3.0  3.0
e  4.0  4.0
f  NaN  5.0
g  5.0  NaN

Upvotes: 0

Christian Sloper
Christian Sloper

Reputation: 7510

Yes, you can use merge for what you want:

df1 =  pd.DataFrame({"C1": ["A","B", "C", "D", "F" ] , "C2": [4,5,8,6,2]})
df2 =  pd.DataFrame({"C1": ["A","C", "D", "E", "G" ], "C2": [8,6,9,4,7]})

pd.merge(df1, df2, on="C1", how="outer").sort_values("C1")

This gives the following

    C1  C2_x C2_y
0   A   4.0 8.0
1   B   5.0 NaN
2   C   8.0 6.0
3   D   6.0 9.0
5   E   NaN 4.0
4   F   2.0 NaN
6   G   NaN 7.0

Upvotes: 1

Related Questions