Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Merging Panda Dataframes

    • 0
    • 0
    • 0
    • 7
    • 0
    • 0
    • 0
    • 106
    Answer it

    I have two dataframes. One is user(UserID, Name) and other one is rating (srcUserId, dstUserId, RatingValue). I have to merge these two dataframes such that in place of srcUserId I get the name of the user corresponding to userid from user dataframe.The format should look like below:-

    Name1, Name2, Rating

    (srcUserId), (dstUserId), (RatingValue)

     

    How should I apply merging on both of these to get the output?

 7 Answer(s)

  • df_new is a variable that store a dataframe.And pandas provides various facilities for easily combining together Series, DataFrame, and Panel objects with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations.And You can concatenate a mix of Series and DataFrames. The Series will be transformed to DataFrames with the column name as the name of the Series in data frame.

  • Hello Kajal,

    The below code will help you in solving your issue:

    #import modules
    
    import datetime as dt
    import pandas as pd
    import scipy as s
    
    #Create a dataframe
    raw_data = {
            'subject_id': ['1', '2', '3', '4', '5'],
            'first_name': ['Shiva ', 'Rahul', 'Raghu', 'Akhi', 'Aaman'], 
            'last_name': ['Thakur', 'singh', 'singh', 'singh', 'singh']}
    df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last_name'])
    df_a
    
    #Create a second dataframe
    raw_data = {
            'subject_id': ['4', '5', '6', '7', '8'],
            'first_name': ['Viren', 'Brijesh', 'Sachin', 'Rakesh', 'Sumit'], 
            'last_name': ['kud', 'Mehra', 'singh', 'Pant', 'singh']}
    df_b = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last_name'])
    df_b
    
    #Create a third dataframe
    raw_data = {
            'subject_id': ['1', '2', '3', '4', '5', '7', '8', '9', '10', '11'],
            'test_id': [51, 15, 15, 61, 16, 14, 15, 1, 61, 16]}
    df_n = pd.DataFrame(raw_data, columns = ['subject_id','test_id'])
    df_n
    
    #Join the two dataframes along rows
    
    pd.concat([df_a, df_b], axis=1)
    
    #Merge two dataframes along the subject_id value
    
    pd.merge(df_new, df_n, on='subject_id')
    
    #Merge two dataframes with both the left and right dataframes using the subject_id key
    
    pd.merge(df_new, df_n, left_on='subject_id', right_on='subject_id')
    #Join the two dataframes along columns
    pd.concat([df_a, df_b], axis=1)
    
    #Merge two dataframes along the subject_id value
    pd.merge(df_new, df_n, on='subject_id')
    
    #Merge two dataframes with both the left and right dataframes using the subject_id key
    pd.merge(df_new, df_n, left_on='subject_id', right_on='subject_id')
    
    
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: