Milo Ventimiglia Milo Ventimiglia - 27 days ago 8
Python Question

Split pandas entry into all single characters and convert it to dataframe

I have imported a txt file into a panda dataframe and the result is as shown below:

0 0101110011110110111001010100001111100010010010...
Name: 0, dtype: object


I want to split that entry into all its individual numbers, and turn into a data frame where each row is each single element of the entry above:

The dataframe shall look like this:

0 0 \\

1 1 \\

2 0 \\

3 1 \\

4 1 \\

5 1 \\

6 0 \\

7 0 \\
....


Note:
0,1,2,3,4,5,6,7
are the row indexes.

Any ideas? Thanks!

Answer Source

Using pandas you can proceed as follows:

pd.DataFrame(df[0].apply(list)[0]) 

What this does is:

  • convert the string 010111....0010 to a list of characters [0,1,0..0,1,0]
  • use the resulting list to create a new dataframe with the format you are looking for.

Reproducible example:

In [26]: df = pd.DataFrame(['0101110011110110111001010100001111100010010010'])

In [27]: df
Out[27]: 
                                                0
0  0101110011110110111001010100001111100010010010

In [28]: pd.DataFrame(df[0].apply(list)[0]).head(10)
Out[28]: 
   0
0  0
1  1
2  0
3  1
4  1
5  1
6  0
7  0
8  1
9  1

In [29]: