Felipe Barbosa
Felipe Barbosa

Reputation: 19

Split a pandas dataframe header into multiple columns

I'm trying to split the dataframe header id;signin_count;status into more columns where I can put my data into. I've tried df.columns.values, but I couldn't get a string to use .split in, as I was hoping. Instead, I got:

Index(['id;signin_count;status'], dtype='object')

Which returns AttributeError: 'Index' object has no attribute 'split' when I try .split

In broader terms, I have:

id;signin_count;status
0   353;20;done;
1   374;94;pending;
2   377;4;done;

And want:

    id      signin_count  status
0   353     20            done
1   374     94            pending
2   377     4             done

Splitting the data itself is not the problem here, that I can do. The focus is on how to access the header names without hardcoding it, as I will have to do the same with any other dataset with the same format

From the get-go, thank you

Upvotes: 1

Views: 1037

Answers (1)

M_S_N
M_S_N

Reputation: 2810

If you are reading your data from a csv file you can define sep to ; and read it as:

df=pd.read_csv('filename.csv', sep=';', index_col=False)

Output:

    id  signin_count    status
0   353     20  done
1   374     94  pending
2   377     4   done

Upvotes: 3

Related Questions