How to extract specific columns in python and render the results in another file

Viewed 156

I'm a beginner in python, and I need your help in a issue, I have 126 files that contains more than 12 columns and more than 1000 lines, I want to create a file which contains column 1 and 2 of all files.

so for example if I have the file 1 which contains 5 columns from A to E

A       B     C     D    E
name1   2     13    98   6
name2   7     8     67   12
name3   56    67    9    7

and the file 2 which contains 5 columns from A to E

A       B     C     D    E
name1   3     13    98   6
name2   9     8     67   12
name3   12    67    9    7

I want to create a final file which contains column A and column B of each file

so the result will be

A       B     B   
name1   2     3 
name2   7     9  
name3   56    12    

Please tell me if you want to know any other informations or clarification Thank you very much

1 Answers

simply merge subsets of the two dataframes on 'A'

df[['A','B']].merge(df2[['A','B']], on=['A'])

The two similar-named columns (B) will have to be renamed, since you can't have two columns with the same name. Default is (“_x”, “_y”).

You can choose your own suffixes by adding suffixes parameter, for example:

df[['A','B']].merge(df2[['A','B']], on=['A'], suffixes=['', '2'])
Related