How can I seperate my one long column name into columns

Question:

I have this CSV file
and this column name

['school;sex;age;address;famsize;Pstatus;Medu;Fedu;Mjob;Fjob;reason;guardian;traveltime;studytime;failures;schoolsup;famsup;paid;activities;nursery;higher;internet;romantic;famrel;freetime;goout;Dalc;Walc;health;absences;G1;G2;G3']

This includes only 1 column but I want to separate them. I tried to use regex. separating them from ; this syntax but I don’t sure how I can do this

Asked By: Sinem Bıyıklı

||

Answers:

if example.csv is mixing separators like

ID,Name,school;sex;age;address
1,Bart,Springfield;M;10;123 terrace st
1,Lisa,Springfield;M;8;123 terrace st

you can use the regexp [,;] to match either

import pandas as pd
pd.read_csv('example.csv', sep='[,;]')

[if the csv file extension is misleading — not actually "comma separated values" but semicolon separated, you can just use sep=';']

but watch out for something like a comments column that might have rows with values containing ; meant as part of the text and not a separator: bart's smart; doesn't apply himself

Answered By: Will
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.