Merge two data frames based on common column values in Pandas

后端 未结 3 648
陌清茗
陌清茗 2020-11-27 16:02

How to get merged data frame from two data frames having common column value such that only those rows make merged data frame having common value in a particular column.

相关标签:
3条回答
  • 2020-11-27 16:28

    We can merge two Data frames in several ways. Most common way in python is using merge operation in Pandas.

    import pandas
    dfinal = df1.merge(df2, on="movie_title", how = 'inner')
    

    For merging based on columns of different dataframe, you may specify left and right common column names specially in case of ambiguity of two different names of same column, lets say - 'movie_title' as 'movie_name'.

    dfinal = df1.merge(df2, how='inner', left_on='movie_title', right_on='movie_name')
    

    If you want to be even more specific, you may read the documentation of pandas merge operation.

    0 讨论(0)
  • 2020-11-27 16:35

    If you want to merge two dataframes and you want a merged data frame in which only common values from both data frames will appear then do inner merge.

    import pandas as pd
    
    merged_Frame = pd.merge(df1,df2, on = id,how=inner)
    
    0 讨论(0)
  • 2020-11-27 16:41

    You can use pd.merge:

    import pandas as pd
    pd.merge(df1, df2, on="movie_title")
    

    Only rows are kept for which common keys are found in both data frames. In case you want to keep all rows from the left data frame and only add values from df2 where a matching key is available, you can use how="left".

    0 讨论(0)
提交回复
热议问题