'merge dataframe of posts with dataframe of comments

I'm trying to wrap my had around how to unite the two dataframes I have. One of posts and one of comments. One column the two have in common is the 'tweet_id'. So, in the comments df you have more rows with the same value for 'tweet_id' as they belong to the same post (which has the same tweet_id with them). I want to have on one row the comment along with the post it belongs to. I tried the merge function, but it did not work.

Here are 3 rows from the comments .csv:

username,date,text,tweet_id,referenced_tweet_type
Mabic45,2022-04-09 03:23:10+00:00,@VDancila_PM @Ferxxo_69 @bobi jjbhvgrdrhhyygft,1511628205714964483,quoted
AlexEneAA,2022-03-24 12:31:16+00:00,@VDancila_PM @AminaJMohammed klops qq,1177657323818508288,replied_to
PAULCHA79384160,2022-03-21 14:45:42+00:00,"@VDancila_PM @AminaJMohammed Cel rău face o lucrare înșelătoare, dar celui ce seamănă dreptate va fi o răsplată sigură.
Dumnezeu sa binecuvanteze Romania,1177657323818508288,replied_to

and the second one with the tweets:

username,date,tweet_id,text
VDancila_PM,2019-09-27 18:52:40+00:00,1177657323818508288,"I discussed today with UN Deputy Secretary-General @AminaJMohammed about sustainable development, a highly important topic nowadays. My full appreciation for actions meant to strengthen #UN Development System that contribute to attaining the goals of the 2030 Agenda.
VDancila_PM,2019-09-24 12:16:06+00:00,1511628205714964483,"RT @DavidHarrisAJC: Met w/ @VDancila_PM, Prime Minister of #Romania.

Expected output:

username_poster, username_commenter, tweet, comment, tweet_id
VDancila_PM,AlexEneAA,"I discussed today with UN Deputy Secretary-General @AminaJMohammed about sustainable development, a highly important topic nowadays. My full appreciation for actions meant to strengthen #UN Development System that contribute to attaining the goals of the 2030 Agenda.,@VDancila_PM @AminaJMohammed klops qq,1177657323818508288
VDancila_PM,PAULCHA79384160,"I discussed today with UN Deputy Secretary-General @AminaJMohammed about sustainable development, a highly important topic nowadays. My full appreciation for actions meant to strengthen #UN Development System that contribute to attaining the goals of the 2030 Agenda.,"@VDancila_PM @AminaJMohammed Cel rău face o lucrare înșelătoare, dar celui ce seamănă dreptate va fi o răsplată sigură.Dumnezeu sa binecuvanteze Romania,1177657323818508288

and this is the full code I tried:

df_tweets_1 = pd.read_csv('tweets.csv')
df_tweets_2 = pd.read_csv('tweets1.csv')
df_tweets = pd.concat([df_tweets_1, df_tweets_2], axis=0)
df_comments = pd.read_csv('comments.csv')
df_comments = df_comments[['username','date','tweet_id','text']]

df_final = df_tweets.merge(df_comments, on='tweet_id', how='left')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source