Home > Software design >  Split one column of a dataframe with xy coordinates into two columns, without a delimeter (every 2 v
Split one column of a dataframe with xy coordinates into two columns, without a delimeter (every 2 v

Time:11-30

I have a .csv with xy coordinates, which then I read with pandas. The problem is that the .csv has the data in only one column (here, the 1st value is a X value, the 2nd value is a Y value, the 3rd value is a X value, and so on) as shown here

This csv is readed with pandas, and the resulting dataframe is in the same format as shown here.

The dataframe which I want to get, is like

     X    |   Y
1) 792.0  |  610.0
2) 786.0  |  602.0
3) ...    |  ...

The problem is that the dataframe/csv doesn't have a delimiter, like ','.

I want to split the one and only column into two columns (called as X and Y), with every two values.

CodePudding user response:

Assuming the number of values is even, you can use:

out = pd.DataFrame(df.iloc[:,0].to_numpy().reshape(-1,2), columns=['X', 'Y'])

Output:

       X      Y
0  792.0  610.0
1  786.0  602.0
  • Related