# creating a csv file from two csv files

I have two csv files:

sub_compiler.to_csv('sub_compiler.csv')


and

sub_opt = pd.read_csv('sub_opt.csv')


and what I would like to do is to create a csv file where I have something of the form

compiler, opt

How could I do this? I need to do this to make a submission.

[EDIT] Thank you for the answers. Now I have obtained :

don't understand why I have the columns called unnamed.

My objective is to get a csv file that contains only the columns compiler and opt. How could I do this? Thank's again.

[EDIT 2] I have solved my problem but if I open the csv file manually, so just click on the file, I have the following:

s it contains only 11 lines, but it contains 3000 lines in jupyter. Is it there something wrong?

[EDIT 3] I tried to to the following:

import pandas as pd

test


and I have:

where 1495927.csv is the csv file I have created and that is in the image where I have 11 elements.

• When you use to_csv try add parameter "index=False" this way when you save it, it will not write the index column in the output file which when you read it will produce the unnamed columns as you saw. Try it first let me know if the problem persists. – Yohanes Alfredo Nov 10 '19 at 10:46
• Thank's I have created the csv file. Is it normal that when I opened my csv file it contains only 11 lines? – J.D. Nov 10 '19 at 15:14
• That is weird. Can you confirm within the code where you create the combined dataframe that you have the correct dataframe? So far as I observe your update it is heading in the right direction. And my previous comment should solve the issue you face at edit 1 – Yohanes Alfredo Nov 10 '19 at 15:25
• I have edited my question. In jupyter it seems correct, but when I ioen manually the csv I have only 11 elements in it. I don' understand why. Thank's. – J.D. Nov 10 '19 at 16:27
• Actually by reloading every cell and opening again the csv it is ok now, I have all the rows. Really thank you again. – J.D. Nov 10 '19 at 16:30

sub_compiler.merge(sub_opt, how='inner', on='instruction')


This is how to do table join on your table.

Another option if it is already aligned and you just want to concatenate you can do

pd.concat([sub_compiler,sub_opt[['opt']]], axis=1)


This is naive column-wise concatenate.

Finally just proceed with to_csv.

Using pd.DataFrame[['column']] will extract a column as a DataFrame. You can do this on sub_compiler and sub_opt to grab the columns you want. Since they share the same index, you can just align them horizontally with pd.concat.

pd.concat([sub_compiler[['compiler']], sub_opt[['opt']]], axis=1)

An alternative to other (correct) answers: it seems the instructions columns are the same for both, so you could just add the opt columns to the dataframe as a new columns and write that. That way you have all information still (and you said you want something like compiler, opt ;)

sub_compiler["opt"] = sub_opt.opt


Then write to disk as before:

sub_compiler.to_csv("compiler_opt.csv")