4

if I have one dataframe and series like these

  bi            sm
0  A          0  a
1  B          1  b
2  C          2  C

I can concatenate like this

dfA['sm'] = dfB

  big  sm
0 A    a
1 B    b 
2 C    c

However I have one dataframe and seriese, each has the different index but same row numbers.

            bi            sm
2017-11-04  A          0  a
2017-11-03  B          1  b
2017-11-02  C          2  C 

I would like to concatenate these two into this

            bi  sm
2017-11-04  A   a
2017-11-03  B   b
2017-11-02  C   C 

How can I make it???

2

2 Answers 2

9

Make both dataframes have the same index, then concatenate:

pd.concat([dfA, dfB.set_index(dfA.index)], axis=1)
#           bi sm
#2017-11-04  A  a
#2017-11-03  B  b
#2017-11-02  C  c
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it works. Is it possible to change the name of column when concatenating?? sm -> md
@whitebear Use pd.concat([dfA, dfB.set_index(dfA.index)], axis=1).rename(columns={'sm' : 'md'})
THank you, change index before concatinating is the very understandable.
4

You can assign dfB.sm as values values

Option 1

In [209]: dfA['sm'] = dfB.sm.values

In [210]: dfA
Out[210]:
           bi sm
2017-11-04  A  a
2017-11-03  B  b
2017-11-02  C  C

Option 2

In [215]: dfA.assign(smm=dfB.sm.values)
Out[215]:
           bi smm
2017-11-04  A   a
2017-11-03  B   b
2017-11-02  C   C

Note: dfA, dfB are both dataframes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.