Home > Enterprise >  How can I seperate my one long column name into columns
How can I seperate my one long column name into columns

Time:11-07

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

CodePudding user response:

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

  • Related