Merge two data frames

  dataframe, merge, python

I tried two merge two data frames by adding the first line of the second df to the first line of the first df. I also tried to concatenate them but eiter failed.
The format of the Data is

1,3,N0128,Durchm.,5.0,0.1,5.0760000000000005,0.076,-----****--
2,0.000,,,,,,,
3,3,N0129,Position,62.2,0.376,62.238,0.136,***---
4,76.1,-36.000,0.300,-36.057,,,,
5,2,N0130,Durchm.,5.0,0.1,5.067,0.067,-----***---
6,0.000,,,,,,,

I already splitted the dataframe from above into two frames. The first one contains only the odd indexes and the second one the even one’s.
My problem is now, to merge/concatenate the two frames, by adding the first row of the second df to the first row of the first df. I already tried some methods of merging/concatenating but all of them failed. All the print functions are not neccessary, I only use them to have a quick overview in the console.
The code which I felt most comfortable with is:

os.chdir(output)
csv_files = os.listdir('.')
for csv_file in (csv_files):
        if csv_file.endswith(".asc.csv"):
            df = pd.read_csv(csv_file)
            keep_col = ['Messpunkt', 'Zeichnungspunkt', 'Eigenschaft', 'Position', 'Sollmass','Toleranz','Abweichung','Lage']
            new_df = df[keep_col]
            new_df = new_df[~new_df['Messpunkt'].isin(['**Teil'])]
            new_df = new_df[~new_df['Messpunkt'].isin(['**KS-Oben'])]
            new_df = new_df[~new_df['Messpunkt'].isin(['**KS-Unten'])]
            new_df = new_df[~new_df['Messpunkt'].isin(['**N'])]
            print(new_df)   
            new_df.to_csv(output+csv_file)     
            
            df1 = new_df[new_df.index % 2 ==1]
            df2 = new_df[new_df.index % 2 ==0]
            print (df1)
            print (df2)
            merge_df = pd.concat([df1,df2], axis=1)
            print (merge_df)
            merge_df.to_csv(output+csv_file)

I highly appreciate some help.

Source: Python Questions

LEAVE A COMMENT