1

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

2
  • You can use df.columns[0].split(';'). columns render a list of values, in your case, all you need is the first value. Commented Feb 1, 2020 at 21:15
  • 1
    M_S_N answered your root problem, you'll probably end up creating new dfs for every split since all the data shape changes Commented Feb 1, 2020 at 21:19

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It's way easier than what I was trying to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.