Pandas groupby transposeConverting a Pandas GroupBy output from Series to DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersGreater/less than comparisons between Pandas DataFrames/Series

PL/SQL function to receive a number and return its binary format

What's the correct term for a waitress in the Middle Ages?

Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP)/sleep apnea device?

Why does VirtualBox crash macOS?

What risks are there when you clear your cookies instead of logging off?

Average spam confidence

Should I "tell" my exposition or give it through dialogue?

Do any instruments not produce overtones?

Disclosing Spiritual Experiences

When writing an error prompt, should we end the sentence with a exclamation mark or a dot?

QGIS Draw by rule

What is the advantage of carrying a tripod and ND-filters when you could use image stacking instead?

Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?

How to make thick Asian sauces?

Traffic law UK, pedestrians

Avoiding cliches when writing gods

You've spoiled/damaged the card

Where does this pattern of naming products come from?

4 Layer PCB stack up

"Living" organ bank is it practical?

SF novella separating the dumb majority from the intelligent part of mankind

My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?

siunitx error: Invalid numerical input

Why only the fundamental frequency component is said to give useful power?



Pandas groupby transpose


Converting a Pandas GroupBy output from Series to DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersGreater/less than comparisons between Pandas DataFrames/Series






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I have a file from SAP which wasn't the prettiest one when dealing with data. So using series.str.contains() and boolean masks I have managed to narrow down to a dataframe looking like below:



 0 1
0 SUB 123
1 CAT SKU
2 CODE 1000123
3 CODE 1000234
4 SUB 456
5 CAT LIQ
6 CODE1 1000345
7 CODE1 1000534
8 CODE1 1000433


I am looking for a way where I can separate each SUB into a new entry like below:



print(expected_df)

SUB CAT CODE CODE1
0 123 SKU 1000123.0 NaN
1 123 SKU 1000234.0 NaN
2 456 LIQ NaN 1000345.0
3 456 LIQ NaN 1000534.0
4 456 LIQ NaN 1000433.0


I just cant seem to get pass this step. However, this line:



df[0].eq('SUB').cumsum()


helps to segregate the groups and can be used as a helper series if needed.



Any help in transposing the data as shown would be really appreciated.



Thanks.










share|improve this question
























  • Is there always one CAT per SUB?

    – Jondiedoop
    Mar 24 at 15:12











  • @Jondiedoop thanks for taking a look, yes there is just CAT per SUB. Basically its like a category. :)

    – anky_91
    Mar 24 at 15:13

















3















I have a file from SAP which wasn't the prettiest one when dealing with data. So using series.str.contains() and boolean masks I have managed to narrow down to a dataframe looking like below:



 0 1
0 SUB 123
1 CAT SKU
2 CODE 1000123
3 CODE 1000234
4 SUB 456
5 CAT LIQ
6 CODE1 1000345
7 CODE1 1000534
8 CODE1 1000433


I am looking for a way where I can separate each SUB into a new entry like below:



print(expected_df)

SUB CAT CODE CODE1
0 123 SKU 1000123.0 NaN
1 123 SKU 1000234.0 NaN
2 456 LIQ NaN 1000345.0
3 456 LIQ NaN 1000534.0
4 456 LIQ NaN 1000433.0


I just cant seem to get pass this step. However, this line:



df[0].eq('SUB').cumsum()


helps to segregate the groups and can be used as a helper series if needed.



Any help in transposing the data as shown would be really appreciated.



Thanks.










share|improve this question
























  • Is there always one CAT per SUB?

    – Jondiedoop
    Mar 24 at 15:12











  • @Jondiedoop thanks for taking a look, yes there is just CAT per SUB. Basically its like a category. :)

    – anky_91
    Mar 24 at 15:13













3












3








3








I have a file from SAP which wasn't the prettiest one when dealing with data. So using series.str.contains() and boolean masks I have managed to narrow down to a dataframe looking like below:



 0 1
0 SUB 123
1 CAT SKU
2 CODE 1000123
3 CODE 1000234
4 SUB 456
5 CAT LIQ
6 CODE1 1000345
7 CODE1 1000534
8 CODE1 1000433


I am looking for a way where I can separate each SUB into a new entry like below:



print(expected_df)

SUB CAT CODE CODE1
0 123 SKU 1000123.0 NaN
1 123 SKU 1000234.0 NaN
2 456 LIQ NaN 1000345.0
3 456 LIQ NaN 1000534.0
4 456 LIQ NaN 1000433.0


I just cant seem to get pass this step. However, this line:



df[0].eq('SUB').cumsum()


helps to segregate the groups and can be used as a helper series if needed.



Any help in transposing the data as shown would be really appreciated.



Thanks.










share|improve this question
















I have a file from SAP which wasn't the prettiest one when dealing with data. So using series.str.contains() and boolean masks I have managed to narrow down to a dataframe looking like below:



 0 1
0 SUB 123
1 CAT SKU
2 CODE 1000123
3 CODE 1000234
4 SUB 456
5 CAT LIQ
6 CODE1 1000345
7 CODE1 1000534
8 CODE1 1000433


I am looking for a way where I can separate each SUB into a new entry like below:



print(expected_df)

SUB CAT CODE CODE1
0 123 SKU 1000123.0 NaN
1 123 SKU 1000234.0 NaN
2 456 LIQ NaN 1000345.0
3 456 LIQ NaN 1000534.0
4 456 LIQ NaN 1000433.0


I just cant seem to get pass this step. However, this line:



df[0].eq('SUB').cumsum()


helps to segregate the groups and can be used as a helper series if needed.



Any help in transposing the data as shown would be really appreciated.



Thanks.







python pandas pandas-groupby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 15:17







anky_91

















asked Mar 24 at 15:07









anky_91anky_91

15.6k41023




15.6k41023












  • Is there always one CAT per SUB?

    – Jondiedoop
    Mar 24 at 15:12











  • @Jondiedoop thanks for taking a look, yes there is just CAT per SUB. Basically its like a category. :)

    – anky_91
    Mar 24 at 15:13

















  • Is there always one CAT per SUB?

    – Jondiedoop
    Mar 24 at 15:12











  • @Jondiedoop thanks for taking a look, yes there is just CAT per SUB. Basically its like a category. :)

    – anky_91
    Mar 24 at 15:13
















Is there always one CAT per SUB?

– Jondiedoop
Mar 24 at 15:12





Is there always one CAT per SUB?

– Jondiedoop
Mar 24 at 15:12













@Jondiedoop thanks for taking a look, yes there is just CAT per SUB. Basically its like a category. :)

– anky_91
Mar 24 at 15:13





@Jondiedoop thanks for taking a look, yes there is just CAT per SUB. Basically its like a category. :)

– anky_91
Mar 24 at 15:13












3 Answers
3






active

oldest

votes


















2














You can try of using df.pivot followed by .ffill(),bfill() for the specific 'SUB' column group rows.



df1 = df.pivot(columns='0')
df1.columns = df1.columns.map(lambda x: x[1])
df1.SUB = df1.SUB.ffill()
df1.groupby('SUB').ffill().groupby('SUB').bfill().drop_duplicates()
#5.89 ms ± 1.84 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

# as time constraints, without use of lambda operation
#df1.groupby(df1.SUB.ffill()).apply(lambda x: x.ffill().bfill()).drop_duplicates()
#16 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)


Out:



 SUB CAT CODE CODE1 SUB
2 123 SKU 1000123 NaN 123
3 123 SKU 1000234 NaN 123
6 456 LIQ NaN 1000345 456
7 456 LIQ NaN 1000534 456
8 456 LIQ NaN 1000433 456





share|improve this answer




















  • 1





    Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

    – anky_91
    Mar 24 at 16:42







  • 1





    Glad to help, happy coding :-) @anky_91

    – Naga Kiran
    Mar 24 at 16:47


















2














IIUC,



df.set_index('col1').groupby(df.col1.eq('SUB').cumsum().values).apply(lambda s: pd.DataFrame(
'SUB': s.loc['SUB'].item(),
'CAT': s.loc['CAT'].item(),
s.index[2]: s.loc[s.index[2]].col2.tolist()
)).reset_index(drop=True)


Outputs



 SUB CAT CODE CODE1
0 123 SKU 1000123 NaN
1 123 SKU 1000234 NaN
2 456 LIQ NaN 1000345
3 456 LIQ NaN 1000534
4 456 LIQ NaN 1000433


However, this looks like an XY problem. Maybe it's worth taking a look into how you ended up with this df in the first place






share|improve this answer























  • Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

    – anky_91
    Mar 24 at 15:32


















2














IIUC



l=[y.set_index('0').T.set_index(['SUB','CAT']).stack() for x , y in df.groupby(df['0'].eq('SUB').cumsum())]
s=pd.concat(l).to_frame('v')
s.assign(key=s.groupby(level=[0,1,2]).cumcount()).set_index('key',append=True).unstack(2)
v
0 CODE CODE1
SUB CAT key
123 SKU 0 1000123 NaN
1 1000234 NaN
456 LIQ 0 NaN 1000345
1 NaN 1000534
2 NaN 1000433





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325191%2fpandas-groupby-transpose%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can try of using df.pivot followed by .ffill(),bfill() for the specific 'SUB' column group rows.



    df1 = df.pivot(columns='0')
    df1.columns = df1.columns.map(lambda x: x[1])
    df1.SUB = df1.SUB.ffill()
    df1.groupby('SUB').ffill().groupby('SUB').bfill().drop_duplicates()
    #5.89 ms ± 1.84 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

    # as time constraints, without use of lambda operation
    #df1.groupby(df1.SUB.ffill()).apply(lambda x: x.ffill().bfill()).drop_duplicates()
    #16 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)


    Out:



     SUB CAT CODE CODE1 SUB
    2 123 SKU 1000123 NaN 123
    3 123 SKU 1000234 NaN 123
    6 456 LIQ NaN 1000345 456
    7 456 LIQ NaN 1000534 456
    8 456 LIQ NaN 1000433 456





    share|improve this answer




















    • 1





      Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

      – anky_91
      Mar 24 at 16:42







    • 1





      Glad to help, happy coding :-) @anky_91

      – Naga Kiran
      Mar 24 at 16:47















    2














    You can try of using df.pivot followed by .ffill(),bfill() for the specific 'SUB' column group rows.



    df1 = df.pivot(columns='0')
    df1.columns = df1.columns.map(lambda x: x[1])
    df1.SUB = df1.SUB.ffill()
    df1.groupby('SUB').ffill().groupby('SUB').bfill().drop_duplicates()
    #5.89 ms ± 1.84 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

    # as time constraints, without use of lambda operation
    #df1.groupby(df1.SUB.ffill()).apply(lambda x: x.ffill().bfill()).drop_duplicates()
    #16 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)


    Out:



     SUB CAT CODE CODE1 SUB
    2 123 SKU 1000123 NaN 123
    3 123 SKU 1000234 NaN 123
    6 456 LIQ NaN 1000345 456
    7 456 LIQ NaN 1000534 456
    8 456 LIQ NaN 1000433 456





    share|improve this answer




















    • 1





      Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

      – anky_91
      Mar 24 at 16:42







    • 1





      Glad to help, happy coding :-) @anky_91

      – Naga Kiran
      Mar 24 at 16:47













    2












    2








    2







    You can try of using df.pivot followed by .ffill(),bfill() for the specific 'SUB' column group rows.



    df1 = df.pivot(columns='0')
    df1.columns = df1.columns.map(lambda x: x[1])
    df1.SUB = df1.SUB.ffill()
    df1.groupby('SUB').ffill().groupby('SUB').bfill().drop_duplicates()
    #5.89 ms ± 1.84 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

    # as time constraints, without use of lambda operation
    #df1.groupby(df1.SUB.ffill()).apply(lambda x: x.ffill().bfill()).drop_duplicates()
    #16 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)


    Out:



     SUB CAT CODE CODE1 SUB
    2 123 SKU 1000123 NaN 123
    3 123 SKU 1000234 NaN 123
    6 456 LIQ NaN 1000345 456
    7 456 LIQ NaN 1000534 456
    8 456 LIQ NaN 1000433 456





    share|improve this answer















    You can try of using df.pivot followed by .ffill(),bfill() for the specific 'SUB' column group rows.



    df1 = df.pivot(columns='0')
    df1.columns = df1.columns.map(lambda x: x[1])
    df1.SUB = df1.SUB.ffill()
    df1.groupby('SUB').ffill().groupby('SUB').bfill().drop_duplicates()
    #5.89 ms ± 1.84 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

    # as time constraints, without use of lambda operation
    #df1.groupby(df1.SUB.ffill()).apply(lambda x: x.ffill().bfill()).drop_duplicates()
    #16 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)


    Out:



     SUB CAT CODE CODE1 SUB
    2 123 SKU 1000123 NaN 123
    3 123 SKU 1000234 NaN 123
    6 456 LIQ NaN 1000345 456
    7 456 LIQ NaN 1000534 456
    8 456 LIQ NaN 1000433 456






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 24 at 16:33

























    answered Mar 24 at 15:49









    Naga KiranNaga Kiran

    2,5491617




    2,5491617







    • 1





      Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

      – anky_91
      Mar 24 at 16:42







    • 1





      Glad to help, happy coding :-) @anky_91

      – Naga Kiran
      Mar 24 at 16:47












    • 1





      Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

      – anky_91
      Mar 24 at 16:42







    • 1





      Glad to help, happy coding :-) @anky_91

      – Naga Kiran
      Mar 24 at 16:47







    1




    1





    Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

    – anky_91
    Mar 24 at 16:42






    Nice. this seems more readable. I will mark this as the answer for now. I will also try implementing in my prod files and let you know. :) Thanks again for your time

    – anky_91
    Mar 24 at 16:42





    1




    1





    Glad to help, happy coding :-) @anky_91

    – Naga Kiran
    Mar 24 at 16:47





    Glad to help, happy coding :-) @anky_91

    – Naga Kiran
    Mar 24 at 16:47













    2














    IIUC,



    df.set_index('col1').groupby(df.col1.eq('SUB').cumsum().values).apply(lambda s: pd.DataFrame(
    'SUB': s.loc['SUB'].item(),
    'CAT': s.loc['CAT'].item(),
    s.index[2]: s.loc[s.index[2]].col2.tolist()
    )).reset_index(drop=True)


    Outputs



     SUB CAT CODE CODE1
    0 123 SKU 1000123 NaN
    1 123 SKU 1000234 NaN
    2 456 LIQ NaN 1000345
    3 456 LIQ NaN 1000534
    4 456 LIQ NaN 1000433


    However, this looks like an XY problem. Maybe it's worth taking a look into how you ended up with this df in the first place






    share|improve this answer























    • Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

      – anky_91
      Mar 24 at 15:32















    2














    IIUC,



    df.set_index('col1').groupby(df.col1.eq('SUB').cumsum().values).apply(lambda s: pd.DataFrame(
    'SUB': s.loc['SUB'].item(),
    'CAT': s.loc['CAT'].item(),
    s.index[2]: s.loc[s.index[2]].col2.tolist()
    )).reset_index(drop=True)


    Outputs



     SUB CAT CODE CODE1
    0 123 SKU 1000123 NaN
    1 123 SKU 1000234 NaN
    2 456 LIQ NaN 1000345
    3 456 LIQ NaN 1000534
    4 456 LIQ NaN 1000433


    However, this looks like an XY problem. Maybe it's worth taking a look into how you ended up with this df in the first place






    share|improve this answer























    • Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

      – anky_91
      Mar 24 at 15:32













    2












    2








    2







    IIUC,



    df.set_index('col1').groupby(df.col1.eq('SUB').cumsum().values).apply(lambda s: pd.DataFrame(
    'SUB': s.loc['SUB'].item(),
    'CAT': s.loc['CAT'].item(),
    s.index[2]: s.loc[s.index[2]].col2.tolist()
    )).reset_index(drop=True)


    Outputs



     SUB CAT CODE CODE1
    0 123 SKU 1000123 NaN
    1 123 SKU 1000234 NaN
    2 456 LIQ NaN 1000345
    3 456 LIQ NaN 1000534
    4 456 LIQ NaN 1000433


    However, this looks like an XY problem. Maybe it's worth taking a look into how you ended up with this df in the first place






    share|improve this answer













    IIUC,



    df.set_index('col1').groupby(df.col1.eq('SUB').cumsum().values).apply(lambda s: pd.DataFrame(
    'SUB': s.loc['SUB'].item(),
    'CAT': s.loc['CAT'].item(),
    s.index[2]: s.loc[s.index[2]].col2.tolist()
    )).reset_index(drop=True)


    Outputs



     SUB CAT CODE CODE1
    0 123 SKU 1000123 NaN
    1 123 SKU 1000234 NaN
    2 456 LIQ NaN 1000345
    3 456 LIQ NaN 1000534
    4 456 LIQ NaN 1000433


    However, this looks like an XY problem. Maybe it's worth taking a look into how you ended up with this df in the first place







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 24 at 15:29









    rafaelcrafaelc

    30.5k83154




    30.5k83154












    • Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

      – anky_91
      Mar 24 at 15:32

















    • Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

      – anky_91
      Mar 24 at 15:32
















    Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

    – anky_91
    Mar 24 at 15:32





    Thanks a lot, yes, actually it looks like an XY, i am aware of that. :) However the file is from SAP and the format in the file is nothing but one line with bad formattings. :) I will look into it and let you know shortly.

    – anky_91
    Mar 24 at 15:32











    2














    IIUC



    l=[y.set_index('0').T.set_index(['SUB','CAT']).stack() for x , y in df.groupby(df['0'].eq('SUB').cumsum())]
    s=pd.concat(l).to_frame('v')
    s.assign(key=s.groupby(level=[0,1,2]).cumcount()).set_index('key',append=True).unstack(2)
    v
    0 CODE CODE1
    SUB CAT key
    123 SKU 0 1000123 NaN
    1 1000234 NaN
    456 LIQ 0 NaN 1000345
    1 NaN 1000534
    2 NaN 1000433





    share|improve this answer



























      2














      IIUC



      l=[y.set_index('0').T.set_index(['SUB','CAT']).stack() for x , y in df.groupby(df['0'].eq('SUB').cumsum())]
      s=pd.concat(l).to_frame('v')
      s.assign(key=s.groupby(level=[0,1,2]).cumcount()).set_index('key',append=True).unstack(2)
      v
      0 CODE CODE1
      SUB CAT key
      123 SKU 0 1000123 NaN
      1 1000234 NaN
      456 LIQ 0 NaN 1000345
      1 NaN 1000534
      2 NaN 1000433





      share|improve this answer

























        2












        2








        2







        IIUC



        l=[y.set_index('0').T.set_index(['SUB','CAT']).stack() for x , y in df.groupby(df['0'].eq('SUB').cumsum())]
        s=pd.concat(l).to_frame('v')
        s.assign(key=s.groupby(level=[0,1,2]).cumcount()).set_index('key',append=True).unstack(2)
        v
        0 CODE CODE1
        SUB CAT key
        123 SKU 0 1000123 NaN
        1 1000234 NaN
        456 LIQ 0 NaN 1000345
        1 NaN 1000534
        2 NaN 1000433





        share|improve this answer













        IIUC



        l=[y.set_index('0').T.set_index(['SUB','CAT']).stack() for x , y in df.groupby(df['0'].eq('SUB').cumsum())]
        s=pd.concat(l).to_frame('v')
        s.assign(key=s.groupby(level=[0,1,2]).cumcount()).set_index('key',append=True).unstack(2)
        v
        0 CODE CODE1
        SUB CAT key
        123 SKU 0 1000123 NaN
        1 1000234 NaN
        456 LIQ 0 NaN 1000345
        1 NaN 1000534
        2 NaN 1000433






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 15:31









        WeNYoBenWeNYoBen

        137k84575




        137k84575



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325191%2fpandas-groupby-transpose%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해