Reputation: 439
I have a dataset which has all the headers written in a continuous format (in the very first cell of the file) without any delimiter and with names having varying length, which looks something like this:
ABCDEFG
1 0 1 0
0 0 1 0
1 1 0 1
I would like the dataset to look something like this:
AB C D EFG
1 0 1 0
0 0 1 0
1 1 0 1
With every column getting its respective header name. How can I do this?
Upvotes: 0
Views: 356
Reputation: 403208
Regardless of how your columns currently are, you can first join them and the resplit using itertools
:
from itertools import islice
seq = [2, 1, 1, 3]
it = iter(''.join(df.columns))
df.columns = [''.join(islice(it, 0, i)) for i in seq]
This assigns ['AB', 'C', 'D', 'EFG']
to df.columns
. The advantage of this technique is you can control the size and number of splits nicely simply by editing the seq
list, and nothing else.
Upvotes: 1
Reputation: 23012
If your header is something like that:
header = ['ABCDEFG']
Based on index, you can write:
header = [header[0][:2], header[0][2], header[0][3], header[0][4:]]
You get:
['AB', 'C', 'D', 'EFG']
Upvotes: 1