python - how to split and concat pandas dataframe -


i have datetime index dataframe of pandas this:

                           b  c  a_1  b_1 2017-07-01 00:00:00  1  34  e    9    0 2017-07-01 00:05:00  2  34  e   92    2 2017-07-01 00:10:00  3  34  e   23    3 2017-07-01 00:15:00  4  34  e    2    5 2017-07-01 00:20:00  5  34  e    4    3 

i want split , concat axis=0, result this

                     c  req  _1 2017-07-01 00:00:00  e  1    9 2017-07-01 00:05:00  e  2   92 2017-07-01 00:10:00  e  3   23 2017-07-01 00:15:00  e  4    2 2017-07-01 00:20:00  e  5    4 2017-07-01 00:00:00  e  34    0 2017-07-01 00:05:00  e  34    2 2017-07-01 00:10:00  e  34    3 2017-07-01 00:15:00  e  34    5 2017-07-01 00:20:00  e  34    3 

so, have this: first, select df[['c','a','a_1']], df[['c','b', 'b_1']]. map columns, , concat result.

it's complicated,is there built-in method in pandas this? or faster method? because have thousands of columns concat final result.

edit

after doing research lreshape not documented , pd.wide_to_long, in current api, same lreshape more flexibility.

https://github.com/pandas-dev/pandas/issues/2567

https://github.com/pandas-dev/pandas/issues/15003

let's use api documented method:

dict1 = {'a':'req_a1','b':'req_b1','a_1':'value_a1','b_1':'value_b1'}  df2 = df1.rename(columns=dict1)  (pd.wide_to_long(df2.reset_index(),['req','value'],i='index',j='c',sep='_',suffix='.')   .rename_axis(['index','dropme'])   .reset_index()   .drop('dropme', axis=1)   .rename(columns={'value':'_1'})) 

output:

                 index  c  req  _1 0  2017-07-01 00:00:00  e    1   9 1  2017-07-01 00:05:00  e    2  92 2  2017-07-01 00:10:00  e    3  23 3  2017-07-01 00:15:00  e    4   2 4  2017-07-01 00:20:00  e    5   4 5  2017-07-01 00:00:00  e   34   0 6  2017-07-01 00:05:00  e   34   2 7  2017-07-01 00:10:00  e   34   3 8  2017-07-01 00:15:00  e   34   5 9  2017-07-01 00:20:00  e   34   3 

use pd.lreshape:

d = {'req': ['a', 'b'], '_1': ['a_1', 'b_1']} df_out = (pd.lreshape(df.reset_index(), d).set_index('index')) 

output:

                     c  req  _1 index                           2017-07-01 00:00:00  e    1   9 2017-07-01 00:05:00  e    2  92 2017-07-01 00:10:00  e    3  23 2017-07-01 00:15:00  e    4   2 2017-07-01 00:20:00  e    5   4 2017-07-01 00:00:00  e   34   0 2017-07-01 00:05:00  e   34   2 2017-07-01 00:10:00  e   34   3 2017-07-01 00:15:00  e   34   5 2017-07-01 00:20:00  e   34   3 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -