TheStrangeQuark
TheStrangeQuark

Reputation: 2405

Append to a DataFrame in Pandas as new column

I have two DataFrames with the same indexing and want to append the second to the first. Lets say I have:

df1 = pd.DataFrame([1,2,3], index = [2,3,4])
df2 = pd.DataFrame([3,5,3], index = [2,3,4])
df1 = df1.append(df2)

which returns

   0
2  1
3  2
4  3
2  3
3  5
4  3

But I want it to append a new column where the indexes match:

2  1  3
3  2  5
4  3  3

Is there a way to do this?

Upvotes: 5

Views: 32836

Answers (2)

vk1011
vk1011

Reputation: 7179

If the indexes match exactly and there's only one column in the other DataFrame (like your question has), then you could even just add the other DataFrame as a new column.

>>> df1['new_column'] = df2
>>> df1
   0  new_column
2  1           3
3  2           5
4  3           3

In general, the concat approach is better. If you have different indexes, you can choose to do an inner join or outer join.

>>> df2 = pd.DataFrame([3,5,3], index = [2,3,5])
>>> df2
   0
2  3
3  5
5  3

>>> pd.concat([df1, df2], axis=1, join='inner')
   0  0
2  1  3
3  2  5

>>> pd.concat([df1, df2], axis=1, join='outer')
    0   0
2   1   3
3   2   5
4   3 NaN
5 NaN   3

Upvotes: 1

EdChum
EdChum

Reputation: 394041

Use concat and pass param axis=1 to concatenate the list of dfs column-wise:

In [3]:

df1 = pd.DataFrame([1,2,3], index = [2,3,4])
df2 = pd.DataFrame([3,5,3], index = [2,3,4])
pd.concat([df1,df2], axis=1)
Out[3]:
   0  0
2  1  3
3  2  5
4  3  3

You can also use join but you have to rename the column first:

In [6]:

df1.join(df2.rename(columns={0:'x'}))
Out[6]:
   0  x
2  1  3
3  2  5
4  3  3

Or merge specifying that you wish to match on indices:

In [8]:

df1.merge(df2.rename(columns={0:'x'}), left_index=True, right_index=True )
Out[8]:
   0  x
2  1  3
3  2  5
4  3  3

Upvotes: 8

Related Questions