JeremyV_07
JeremyV_07

Reputation: 13

Splitting each item in an array into a separate data frame column

I'm relatively new to python. I have a data frame and I need to split each character of the data in each column into it's own column in another data frame. I split the data into a dictionary, but just found that I need a new data frame. Here's the deal:

The source data frame looks like this:

    Col1
1 100100
2 000000
3 020001
4 100300

I have a dictionary like this:

1: "['1', '0', '0', '1', '0', '0']",
2: "['0', '0', '0', '0', '0', '0']",
3: "['0', '2', '0', '0', '0', '1']",
4: "['1', '0', '0', '3', '0', '0']"

and need to end up with a data frame in this format:

  0 1 2 3 4 5
1 1 0 0 1 0 0
2 0 0 0 0 0 0
3 0 2 0 0 0 1
4 1 0 0 3 0 0

Any advice would be appreciated - I haven't had any luck in my searches. I'd assume going direct from the source data to the new data frame is ideal. Or is using the dictionary I created (source==>dict==>new data frame) a better route? Thanks.

Upvotes: 0

Views: 1668

Answers (1)

DSM
DSM

Reputation: 353379

It's not the most elegant, but life is short, so I'd apply list to get the values and then pd.Series to expand them into columns:

>>> df
     Col1
1  100100
2  000000
3  020001
4  100300
>>> df.Col1.apply(list).apply(pd.Series).astype(int)
   0  1  2  3  4  5
1  1  0  0  1  0  0
2  0  0  0  0  0  0
3  0  2  0  0  0  1
4  1  0  0  3  0  0

Upvotes: 3

Related Questions