I have a program that I need to merge two cells into a singular cell. The input .csv file is shown below
12 00 0E 00 57 23
57 23 02 23 57 0A
2D 16 0C 5A 2D 16
This is a small excerpt of the input file I have. I am currently trying to create a new column where the values are
0012
000E
2357
2357
2302
0A57
162D
5A0C
162D
ie the second value precedes the first, is combined, and continues down the input file, left and down. I am not very familiar with Pandas and was hoping there was anyone with any ideas with a solution. Thanks
CodePudding user response:
You can use the underlying numpy array and reshape
:
import io
t = '''12 00 0E 00 57 23
57 23 02 23 57 0A
2D 16 0C 5A 2D 16'''
# using io here for the example but use a file in the real use case
df = pd.read_csv(io.StringIO(t), sep=' ', header=None, dtype=str)
x,y = df.shape
(pd.DataFrame(df.values.reshape(x*y//2, 2)[:, ::-1])
.apply(''.join, axis=1)
)
output:
0 0012
1 000E
2 2357
3 2357
4 2302
5 0A57
6 162D
7 5A0C
8 162D
dtype: object