Rajsxx
Rajsxx

Reputation: 39

Splitting the values of column in csv and write in new column using Pandas

I have an excel column as shown below :-

FileName               Coordinates
abc.text               0 0.41, 0.42, 0.43, 0.44

I want the output to be in this fashion :-

FileName               Coordinates                   Label    X-1      Y-1      X-3      X-4
abc.txt                0, 0.41, 0.42, 0.43, 0.44       0      0.41     0.42     0.43     0.44

I have written the below code and before it worked for me, dont know what I am mission in this specific case :-

import pandas as pd


df = pd.read_csv('path/to/Coordinates_v3_updated.csv')

df[['Label', 'x1','y1', 'x2', 'y2']] = df['Coordinates'].str.split(" ",expand=True)

print(df)

df.to_csv('path/to/save/to/Coordinates_v3_updated_v1.csv', index=False)
print("Done")

Upvotes: 0

Views: 26

Answers (1)

itprorh66
itprorh66

Reputation: 3288

replace df[['Label', 'x1','y1', 'x2', 'y2']] = df['Coordinates'].str.split(" ",expand=True)

with df[['Label', 'x1','y1', 'x2', 'y2']] = df['Coordinates'].str.split(", ",expand=True)

Upvotes: 1

Related Questions