0

I do have two csv files, I am using pandas to read the data.

The train.csv contains values, with headers id, sentiment

87,Positive
10,Positive
7,Neutral

The text.csv contains values, with headers id, text

7,hello, I think the price if high...
87, you can call me tomorow...
....

I would like to insert the text from text.csv into train.csv so the result would be:

87,Positive, you can call me tomorow...

Can any one help with pandas?

import pandas as pd

train= pd.read_csv("train.csv")
text= pd.read_csv("text.csv")

# this does not work
combined= pd.merge(train, text, on=['id'])

Note Some Ids may not be in the files, so I need to set null if the id does not exists

2 Answers 2

1

set the indices on the two dataframes, then add the columns:

train.set_index('id').sentiment + text.set_index('id').text
Sign up to request clarification or add additional context in comments.

2 Comments

the files already have headers, I tried but does not work, also, it may happen that some IDs not exists
always shows empty, the 3 headers show up, but not data ?!
0

One of the easy way can be

pd.merge(train, test, on='id', how='outer')

As per pandas docs, if you use how as outer, it will take all keys

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.