1

I am trying to combine two dataframes, i have date column in df1 and date1 column in df2. i want to compare first value of df1 column date to all value of df2 date1 column if similar value found in date2 just combine similar value row of df2 to the first row of df1 date. then do same for second value of df1 date column and so on.... if value found more than one time add rows more than one

I already tried for loop and if conditions but i am getting very strange result, many rows with NAN ,and dataframe rows increase

all_df=pd.DataFrame()

df1=pd.read_csv('.csv')

df2=pd.read_csv('.csv')

for i in range(len(df1)):

    for j in range(len(df2)):

        if df1['date1'].iloc[i] == df2['date'].iloc[j]:        
            print('yes')


            df=pd.concat([df1.iloc[[i]],df2.iloc[[j]]],axis=1)
            all_df=all_df.append(df)
        else:
            print('no')

i only want rows where df1 date and df2 date2 is same.

lexual
  • 35,478
  • 2
  • 11
  • 13
Nickel
  • 540
  • 2
  • 10

1 Answers1

0

https://pandas.pydata.org/pandas-docs/stable/merging.html#database-style-dataframe-joining-merging

import pandas as pd
all_df = pd.merge(df1, df2, left_on='date1', right_on='date')
lexual
  • 35,478
  • 2
  • 11
  • 13
  • its ok when condition column we have one column in df1 and one in df2 , but how about the situation if we have df1 date1,date2 and df2 date3 , date4 ? – Nickel Dec 20 '18 at 04:59