How do I sort a matrix via two indepedent columns?












0















I've recently started programming and I was tasked with finding the mean of different responses of a drug based on its dosage vs a placebo. I have a 4 column Excel file which is split into Drug_1_Dosage, Drug_1_Response, Drug_2_Dosage, Drug_2_Response, respectively.



I'm trying to have those values sorted in Jupyter notebook so that all instances where Drug_1_Dosage was 1-8 are sorted in an ascending manner for the response (there was more than 1 row with dosage 1 for example), while also doing the same for Drug_2_Dosage (but without affecting the first one).



I want to sort them so that I can calculate the mean later and express it as a two-column matrix.



So far I have this:



import numpy as np
import pandas as pd

file = open('/Users/Envy/Desktop/Assingment Data.csv',"r")
table = pd.read_csv('/Users/Envy/Desktop/Assingment Data.csv')
drug_1_d = table.iloc[:,0]
drug_1_r = table.iloc[:,1]
drug_2_d = table.iloc[:,2]
drug_2_r = table.iloc[:,3]


Up to here everything is working fine as I can select each column independently. I tried the following for sorting to no success:



1) table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
table = table[table[:,0].argsort()]
2) table.sort(order=['f1'],axis=0)
3) table.sort_values(['Dose drug 1', 'Dose drug 2'], ascending=[True])
4) table = table.sort_values([:0,:2],ascending=[True])


EDIT:



Hey, I did some prodding around and this works with the above code



table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
print(table)


But it returns



[[ 1 21  3 27]
[ 1 19 7 10]
[ 1 32 3 12]
...
[ 8 18 4 24]
[ 8 9 1 10]
[ 8 13 2 9]]


Meaning that it only sorts by column 0 and not by column 2 also like I want it. Any idea how I can have both sorts independently?



Edit: After a lot of trial and error I now have the solution;



#Generate average response to dosage in 2 column matrix
table = pd.read_csv('Assingment Data.csv', sep=',')
final_means = pd.DataFrame()
# Grouping by Drug 1
final_means['Average Response Drug'] = table.groupby(['Dose drug 1'])['Response drug 1'].mean()
# Grouping by Drug 2
final_means['Average Response Placebo'] = table.groupby(['Dose drug 2'])['Response drug 2'].mean()
final_means.index.names = ['Dose']
print(final_means)









share|improve this question

























  • Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table

    – Energya
    Nov 22 '18 at 12:01













  • Thank you so much! This with a bit of tinkering solved it. I added solution for future asks

    – Email for rat facts
    Nov 23 '18 at 15:31






  • 1





    @Energya Please submit your comment as a solution to the question.

    – Sean Pianka
    Nov 27 '18 at 22:31
















0















I've recently started programming and I was tasked with finding the mean of different responses of a drug based on its dosage vs a placebo. I have a 4 column Excel file which is split into Drug_1_Dosage, Drug_1_Response, Drug_2_Dosage, Drug_2_Response, respectively.



I'm trying to have those values sorted in Jupyter notebook so that all instances where Drug_1_Dosage was 1-8 are sorted in an ascending manner for the response (there was more than 1 row with dosage 1 for example), while also doing the same for Drug_2_Dosage (but without affecting the first one).



I want to sort them so that I can calculate the mean later and express it as a two-column matrix.



So far I have this:



import numpy as np
import pandas as pd

file = open('/Users/Envy/Desktop/Assingment Data.csv',"r")
table = pd.read_csv('/Users/Envy/Desktop/Assingment Data.csv')
drug_1_d = table.iloc[:,0]
drug_1_r = table.iloc[:,1]
drug_2_d = table.iloc[:,2]
drug_2_r = table.iloc[:,3]


Up to here everything is working fine as I can select each column independently. I tried the following for sorting to no success:



1) table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
table = table[table[:,0].argsort()]
2) table.sort(order=['f1'],axis=0)
3) table.sort_values(['Dose drug 1', 'Dose drug 2'], ascending=[True])
4) table = table.sort_values([:0,:2],ascending=[True])


EDIT:



Hey, I did some prodding around and this works with the above code



table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
print(table)


But it returns



[[ 1 21  3 27]
[ 1 19 7 10]
[ 1 32 3 12]
...
[ 8 18 4 24]
[ 8 9 1 10]
[ 8 13 2 9]]


Meaning that it only sorts by column 0 and not by column 2 also like I want it. Any idea how I can have both sorts independently?



Edit: After a lot of trial and error I now have the solution;



#Generate average response to dosage in 2 column matrix
table = pd.read_csv('Assingment Data.csv', sep=',')
final_means = pd.DataFrame()
# Grouping by Drug 1
final_means['Average Response Drug'] = table.groupby(['Dose drug 1'])['Response drug 1'].mean()
# Grouping by Drug 2
final_means['Average Response Placebo'] = table.groupby(['Dose drug 2'])['Response drug 2'].mean()
final_means.index.names = ['Dose']
print(final_means)









share|improve this question

























  • Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table

    – Energya
    Nov 22 '18 at 12:01













  • Thank you so much! This with a bit of tinkering solved it. I added solution for future asks

    – Email for rat facts
    Nov 23 '18 at 15:31






  • 1





    @Energya Please submit your comment as a solution to the question.

    – Sean Pianka
    Nov 27 '18 at 22:31














0












0








0


0






I've recently started programming and I was tasked with finding the mean of different responses of a drug based on its dosage vs a placebo. I have a 4 column Excel file which is split into Drug_1_Dosage, Drug_1_Response, Drug_2_Dosage, Drug_2_Response, respectively.



I'm trying to have those values sorted in Jupyter notebook so that all instances where Drug_1_Dosage was 1-8 are sorted in an ascending manner for the response (there was more than 1 row with dosage 1 for example), while also doing the same for Drug_2_Dosage (but without affecting the first one).



I want to sort them so that I can calculate the mean later and express it as a two-column matrix.



So far I have this:



import numpy as np
import pandas as pd

file = open('/Users/Envy/Desktop/Assingment Data.csv',"r")
table = pd.read_csv('/Users/Envy/Desktop/Assingment Data.csv')
drug_1_d = table.iloc[:,0]
drug_1_r = table.iloc[:,1]
drug_2_d = table.iloc[:,2]
drug_2_r = table.iloc[:,3]


Up to here everything is working fine as I can select each column independently. I tried the following for sorting to no success:



1) table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
table = table[table[:,0].argsort()]
2) table.sort(order=['f1'],axis=0)
3) table.sort_values(['Dose drug 1', 'Dose drug 2'], ascending=[True])
4) table = table.sort_values([:0,:2],ascending=[True])


EDIT:



Hey, I did some prodding around and this works with the above code



table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
print(table)


But it returns



[[ 1 21  3 27]
[ 1 19 7 10]
[ 1 32 3 12]
...
[ 8 18 4 24]
[ 8 9 1 10]
[ 8 13 2 9]]


Meaning that it only sorts by column 0 and not by column 2 also like I want it. Any idea how I can have both sorts independently?



Edit: After a lot of trial and error I now have the solution;



#Generate average response to dosage in 2 column matrix
table = pd.read_csv('Assingment Data.csv', sep=',')
final_means = pd.DataFrame()
# Grouping by Drug 1
final_means['Average Response Drug'] = table.groupby(['Dose drug 1'])['Response drug 1'].mean()
# Grouping by Drug 2
final_means['Average Response Placebo'] = table.groupby(['Dose drug 2'])['Response drug 2'].mean()
final_means.index.names = ['Dose']
print(final_means)









share|improve this question
















I've recently started programming and I was tasked with finding the mean of different responses of a drug based on its dosage vs a placebo. I have a 4 column Excel file which is split into Drug_1_Dosage, Drug_1_Response, Drug_2_Dosage, Drug_2_Response, respectively.



I'm trying to have those values sorted in Jupyter notebook so that all instances where Drug_1_Dosage was 1-8 are sorted in an ascending manner for the response (there was more than 1 row with dosage 1 for example), while also doing the same for Drug_2_Dosage (but without affecting the first one).



I want to sort them so that I can calculate the mean later and express it as a two-column matrix.



So far I have this:



import numpy as np
import pandas as pd

file = open('/Users/Envy/Desktop/Assingment Data.csv',"r")
table = pd.read_csv('/Users/Envy/Desktop/Assingment Data.csv')
drug_1_d = table.iloc[:,0]
drug_1_r = table.iloc[:,1]
drug_2_d = table.iloc[:,2]
drug_2_r = table.iloc[:,3]


Up to here everything is working fine as I can select each column independently. I tried the following for sorting to no success:



1) table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
table = table[table[:,0].argsort()]
2) table.sort(order=['f1'],axis=0)
3) table.sort_values(['Dose drug 1', 'Dose drug 2'], ascending=[True])
4) table = table.sort_values([:0,:2],ascending=[True])


EDIT:



Hey, I did some prodding around and this works with the above code



table = table.sort_values(['Dose drug 1', 'Dose drug 1'],ascending = True)
table = pd.DataFrame.as_matrix(table)
print(table)


But it returns



[[ 1 21  3 27]
[ 1 19 7 10]
[ 1 32 3 12]
...
[ 8 18 4 24]
[ 8 9 1 10]
[ 8 13 2 9]]


Meaning that it only sorts by column 0 and not by column 2 also like I want it. Any idea how I can have both sorts independently?



Edit: After a lot of trial and error I now have the solution;



#Generate average response to dosage in 2 column matrix
table = pd.read_csv('Assingment Data.csv', sep=',')
final_means = pd.DataFrame()
# Grouping by Drug 1
final_means['Average Response Drug'] = table.groupby(['Dose drug 1'])['Response drug 1'].mean()
# Grouping by Drug 2
final_means['Average Response Placebo'] = table.groupby(['Dose drug 2'])['Response drug 2'].mean()
final_means.index.names = ['Dose']
print(final_means)






python columnsorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 22:20







Email for rat facts

















asked Nov 21 '18 at 19:10









Email for rat factsEmail for rat facts

52




52













  • Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table

    – Energya
    Nov 22 '18 at 12:01













  • Thank you so much! This with a bit of tinkering solved it. I added solution for future asks

    – Email for rat facts
    Nov 23 '18 at 15:31






  • 1





    @Energya Please submit your comment as a solution to the question.

    – Sean Pianka
    Nov 27 '18 at 22:31



















  • Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table

    – Energya
    Nov 22 '18 at 12:01













  • Thank you so much! This with a bit of tinkering solved it. I added solution for future asks

    – Email for rat facts
    Nov 23 '18 at 15:31






  • 1





    @Energya Please submit your comment as a solution to the question.

    – Sean Pianka
    Nov 27 '18 at 22:31

















Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table

– Energya
Nov 22 '18 at 12:01







Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table

– Energya
Nov 22 '18 at 12:01















Thank you so much! This with a bit of tinkering solved it. I added solution for future asks

– Email for rat facts
Nov 23 '18 at 15:31





Thank you so much! This with a bit of tinkering solved it. I added solution for future asks

– Email for rat facts
Nov 23 '18 at 15:31




1




1





@Energya Please submit your comment as a solution to the question.

– Sean Pianka
Nov 27 '18 at 22:31





@Energya Please submit your comment as a solution to the question.

– Sean Pianka
Nov 27 '18 at 22:31












1 Answer
1






active

oldest

votes


















0














Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table



A simple example:



>>> import pandas as pd
>>> df = pd.DataFrame(data={'col1': [0,0,1,1], 'col2': [1,2,3,4]})
>>> df
col1 col2
0 0 1
1 0 2
2 1 3
3 1 4
>>> df.groupby('col1').mean()
col2
col1
0 1.5
1 3.5


(Thanks to @Sean-Pianka for suggesting I post my comment as a separate answer)






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%2f53419021%2fhow-do-i-sort-a-matrix-via-two-indepedent-columns%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table



    A simple example:



    >>> import pandas as pd
    >>> df = pd.DataFrame(data={'col1': [0,0,1,1], 'col2': [1,2,3,4]})
    >>> df
    col1 col2
    0 0 1
    1 0 2
    2 1 3
    3 1 4
    >>> df.groupby('col1').mean()
    col2
    col1
    0 1.5
    1 3.5


    (Thanks to @Sean-Pianka for suggesting I post my comment as a separate answer)






    share|improve this answer




























      0














      Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table



      A simple example:



      >>> import pandas as pd
      >>> df = pd.DataFrame(data={'col1': [0,0,1,1], 'col2': [1,2,3,4]})
      >>> df
      col1 col2
      0 0 1
      1 0 2
      2 1 3
      3 1 4
      >>> df.groupby('col1').mean()
      col2
      col1
      0 1.5
      1 3.5


      (Thanks to @Sean-Pianka for suggesting I post my comment as a separate answer)






      share|improve this answer


























        0












        0








        0







        Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table



        A simple example:



        >>> import pandas as pd
        >>> df = pd.DataFrame(data={'col1': [0,0,1,1], 'col2': [1,2,3,4]})
        >>> df
        col1 col2
        0 0 1
        1 0 2
        2 1 3
        3 1 4
        >>> df.groupby('col1').mean()
        col2
        col1
        0 1.5
        1 3.5


        (Thanks to @Sean-Pianka for suggesting I post my comment as a separate answer)






        share|improve this answer













        Are you familiar with Pandas' groupby operation? This allows you to reason per group without having to explicitly sort your table



        A simple example:



        >>> import pandas as pd
        >>> df = pd.DataFrame(data={'col1': [0,0,1,1], 'col2': [1,2,3,4]})
        >>> df
        col1 col2
        0 0 1
        1 0 2
        2 1 3
        3 1 4
        >>> df.groupby('col1').mean()
        col2
        col1
        0 1.5
        1 3.5


        (Thanks to @Sean-Pianka for suggesting I post my comment as a separate answer)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 23:34









        EnergyaEnergya

        879715




        879715
































            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%2f53419021%2fhow-do-i-sort-a-matrix-via-two-indepedent-columns%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)