-
Merging Panda Dataframes
over 8 years ago
-
over 8 years ago
also let me know, which version are you using and running this code on: python 2.7.3 or other.
-
-
over 8 years ago
Send your complete code packet, so that I can check what exactly the issues is.
-
-
over 8 years ago
I'm getting an error while running code saying it cant find df_new.What should I do?
-
-
over 8 years ago
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.
-
-
over 8 years ago
@siva.singh
what is df_new in your code?
-
-
over 8 years ago
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')
-
-
over 8 years ago
....
-
7 Answer(s)