I am trying to match csv entries and extract data but stuck. My csv files are in this format:
df1 looks like this:
type prediction ax ay az
df2 looks like this:
type ax ay az x y z fx fy fz
I would like to first match df1 and df2. For this, I need to match ax, ay and az all together with `df2. Matching only single column can give me wrong dataframe because entries are repeated.
After matching multiple columns with df2, I would like to extract those values and make a dataframe with df1.
Expected dataframe:
type prediction ax ay az x y z
df1 and df2 doesn't have same size. Actually, df2 is a huge file that is why I want to extract only required dataset.
This is my code:
def match_dataset(df1, df2):
df1_new = pd.DataFrame(columns=['x','y','z','fx','fy','fz','az','ax','ay'])
df2_new = pd.DataFrame(columns=['x','y','z','fx','fy','fz','az','ax','ay'])
for i in range(len(df1)):
for j in range(len(df2)):
if df1.iloc[i]['az'] == df2.iloc[j]['az'] and df1.iloc[i]['ay'] == df2.iloc[j]['ay'] and df1.iloc[i]['ax'] == df2.iloc[j]['ax']:
df1_new = df1_new.append(df2.iloc[j], ignore_index=True)
#df2_new = df2_new.append(df2.iloc[j], ignore_index=True)
return df1_new
data = match_dataset(df1, df2)
print(data.head())
But my code is stuck in loop. It doesn't give me output.
Can I get some help? Thank you.