1

i have some data like

>>> df1
  Col1     Col2
0   Hi    Hello
1  How     Fine
2  Why  Because
3  You       Me
4   We     They
>>> df2
      Col1   Col2
0       Mr    Mrs
1  Welcome    Bye
2      Wow    Ohh
3      Sit  Stand

I want to concatenate only the first Column Col1 of these two dataframes and update the df1 leaving Col2 unchanged but i am unable to do it

i tried many ways but failed doing this

Output I want

>>> df2
  Col1     Col2
0   Hi    Hello
1  How     Fine
2  Why  Because
3  You       Me
4  We     They
5  Mr
6  Welcome
7  Wow
8  Sit

It doesnt matter if index is different or there comes NaN instead of Empty Cells

Things i Tried

1

>>> r=pd.concat([df1["Col1"],df2["Col1"]])
>>> df1["Col1"] = r.reset_index(drop=True)
>>> df1
  Col1     Col2
0   Hi    Hello
1  How     Fine
2  Why  Because
3  You       Me
4   We     They

2

>>> pd.concat([df1["Col1"],df2])
     0     Col1   Col2
0   Hi      NaN    NaN
1  How      NaN    NaN
2  Why      NaN    NaN
3  You      NaN    NaN
4   We      NaN    NaN
0  NaN       Mr    Mrs
1  NaN  Welcome    Bye
2  NaN      Wow    Ohh
3  NaN      Sit  Stand

3

I Tried append(), merge() and some more techniques but failed

1 Answer 1

3

append

df1.append(df2[['Col1']])

      Col1     Col2
0       Hi    Hello
1      How     Fine
2      Why  Because
3      You       Me
4       We     They
0       Mr      NaN
1  Welcome      NaN
2      Wow      NaN
3      Sit      NaN

concat

pd.concat([df1, df2[['Col1']]])

      Col1     Col2
0       Hi    Hello
1      How     Fine
2      Why  Because
3      You       Me
4       We     They
0       Mr      NaN
1  Welcome      NaN
2      Wow      NaN
3      Sit      NaN

In either case, you can pass the ignore_index=True option to ignore the index.

df1.append(df2[['Col1']], ignore_index=True)

      Col1     Col2
0       Hi    Hello
1      How     Fine
2      Why  Because
3      You       Me
4       We     They
5       Mr      NaN
6  Welcome      NaN
7      Wow      NaN
8      Sit      NaN
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.