Extract data from multiple bracket string in Pandas and create new table
<p>I am trying to build a 2 x 24 table in pandas with the following data below: </p>

<pre><code>d.iloc[0:2] = [[0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L], [0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 2L, 2L, 0L, 0L, 0L]]
</code></pre>

<p>Basically, the first sub-bracket represents 24 hour data for one day in January, and the second sub-bracket for Febuary. 
I am looking to structure the 2x24 table (without 'L') in the following fashion:</p>

<pre><code>    1 2 3 4 5 6 7 8 9 10 11 12 ... 24
Jan 0 0 0 0 0 0 0 0 0 1  1  1  ... 0
Feb 0 0 0 0 0 0 0 0 0 1  1  1  ... 0
</code></pre>

<p>What I find challenging is stripping (.strip), splitting, and copying the data to a new dataframe structure. I often find the original structure on online dataframes with 12 sub-brackets (one for each month).
I included <code>d.iloc[0,2]</code> because I am going to apply the function to all elements in column 2 with a <code>for</code> loop. 
Thank you for your precious help. </p>

<p>I think you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_records.html" rel="nofollow"><code>DataFrame.from_records</code></a> with apply <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.strip.html" rel="nofollow"><code>str.strip</code></a>:</p>

<pre><code>import pandas as pd
import numpy as np

a = [['0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '1L', '1L', '1L', '1L', '1L', '0L', '0L', '0L', '1L', '1L', '1L', '1L', '0L', '0L', '0L'], 
     ['0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '1L', '1L', '1L', '1L', '1L', '0L', '0L', '0L', '1L', '1L', '2L', '2L', '0L', '0L', '0L']]

idx = ['Jan','Feb']
df = pd.DataFrame.from_records(a, index=idx).apply(lambda x: x.str.strip('L').astype(int))
print (df)
     0   1   2   3   4   5   6   7   8   9  ...  14  15  16  17  18  19  20  \
Jan   0   0   0   0   0   0   0   0   0   1 ...   0   0   0   1   1   1   1   
Feb   0   0   0   0   0   0   0   0   0   1 ...   0   0   0   1   1   2   2   

     21  22  23  
Jan   0   0   0  
Feb   0   0   0  

[2 rows x 24 columns]
</code></pre>

<p>More general solution with generating months names by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.strftime.html" rel="nofollow"><code>dt.strftime</code></a>:</p>

<pre><code>print (pd.Series(range(1,len(a) + 1)))
0    1
1    2
dtype: int32

idx = pd.to_datetime(pd.Series(range(1,len(a) + 1)), format='%m').dt.strftime('%b')
0    Jan
1    Feb
dtype: object

df = pd.DataFrame.from_records(a, index=idx).apply(lambda x: x.str.strip('L').astype(int))
print (df)
     0   1   2   3   4   5   6   7   8   9  ...  14  15  16  17  18  19  20  \
Jan   0   0   0   0   0   0   0   0   0   1 ...   0   0   0   1   1   1   1   
Feb   0   0   0   0   0   0   0   0   0   1 ...   0   0   0   1   1   2   2   

     21  22  23  
Jan   0   0   0  
Feb   0   0   0 
</code></pre>

<p>If need <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html" rel="nofollow"><code>split</code></a> values first:</p>

<pre><code>b = [['0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L'], 
     ['0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 2L, 2L, 0L, 0L, 0L']]

idx = pd.to_datetime(pd.Series(range(1,len(a) + 1)), format='%m').dt.strftime('%b')

df1 = pd.DataFrame.from_records(b, index=idx)
        .iloc[:,0]
        .str.split(', ', expand=True)
        .replace({'L':''}, regex=True)
        .astype(int)
print (df1)

     0   1   2   3   4   5   6   7   8   9  ...  14  15  16  17  18  19  20  \
Jan   0   0   0   0   0   0   0   0   0   1 ...   0   0   0   1   1   1   1   
Feb   0   0   0   0   0   0   0   0   0   1 ...   0   0   0   1   1   2   2   

     21  22  23  
Jan   0   0   0  
Feb   0   0   0  

[2 rows x 24 columns]
</code></pre>

python pandas data-structures extract reshape