2

I have two different csv sample files:

first.csv

name,school
Derrick, schoolA
Peter,schoolB

second.csv

age  ,address
21,abc
22,sdc

I need to combine the two csv files to one. So the result expected is :

combined.csv

 name,school,age  ,address
    Derrick,schoolA, 21,abc
    Peter,schoolB, 22,sdc

Is there a way I can achieve this using pandas?

1
  • What did you try so far? Please provide a sample snippet to show where you are stuck at the moment since SO is not a code writing platform. Long story short: It is possible using pd.concat if you make sure that both dataframes are of equal length. Commented Feb 19, 2020 at 9:45

1 Answer 1

1

You can do something like this.

df1 = pd.read_csv('first.csv')
df2 = pd.read_csv('second.csv')

combined_df = pd.concat([df1, df2], axis=1)

But you need to make sure that df1 and df2 have the same amount of rows. Hope that helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) This actually helps. Just one question. How can I generate a csv file from combined_df?

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.