Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge content of two csv files in alternate rows

I have two CSV files
csv1:

header
a
b
c

csv2:\

header
e
f
g

I want to merge these two files to another CSV in alternate rows like
output.csv:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

header
a
e
b
f
c
g

Can this be done? Thanks in advance

>Solution :

Will definitly not be the fastest way but it works

import pandas as pd
from itertools import chain, zip_longest

x = pd.DataFrame()
x["header"] = [1, 3, 5, 7]

y = pd.DataFrame()
y["header"] = [2, 4, 6, 8, 10, 21]

chained = list(chain.from_iterable(zip_longest(x["header"].to_list(), y["header"].to_list())))
df = pd.DataFrame()
df["header"] = chained
df = df.dropna()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading