Pandas: How do I read specific columns in a file and make them into a new csv

Question:

Here is sample 1 :

| district_id | date        |
| --------    | ----------- |
| 18          | 1995-03-24  |
| 1           | 1993-02-26  |

Sample 2:

| link_id   | type        |
| --------  | ----------- |
| 9         | gold        |
| 19        | classic     |

I want to gather sample 1’s date column and sample 2’s type column and output them as data.csv

Asked By: QZhu

||

Answers:

You can use vertical concatenation of dataframes and then render it:

df3 = pandas.concat([df1['date'], df2['type']], axis = 1)
df3.to_csv('data.csv')
Answered By: César Debeunne
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.