kage77 kage77 - 4 months ago 15
Python Question

How to split merged cells in a CSV file using python

Is there any way to split/unmerge cells in a CSV file using python? What I want is explained below -

enter image description here

The result should a new CSV file with following entries -

enter image description here

The text version looks somewhat like this:

Column1,Column2,Column3,Column4,Column5,
A,A1,A11,A111,A1111,
B,B1,B11,B111,B1111,
,,,,B1112,
,,,,B1113,
,,,,B1114,
,,,,,
,,,,,


Column1,Column2,Column3,Column4,Column5,
A,A1,A11,A111,A1111,
B,B1,B11,B111,B1111,
B,B1,B11,B111,B1112,
B,B1,B11,B111,B1113,
B,B1,B11,B111,B1114,
,,,,,


I understand that there's a solution to unmerge excel workbooks, but I havent found a way to do this for CSV files. Any help will be appreciated.

Answer

It can be done fairly simply with pandas:

d = pandas.read_csv('your_file.csv')
d.fillna(method='ffill', inplace=True)
d.to_csv('new_file.csv', index=False)

Basically this just forward-fills empty cells in each column with the most recent real value.

Comments