How to calculate mean of specific rows based on value and column in numpy matrix?










-1















I'm loading in a file to a pandas dataframe that looks something like:



A 3 2 4 1
B 1 3 5 2
C 2 8 9 1
A 4 1 2 3


I converted the dataframe to a numpy matrix because I'd like to store each mean and variance in separate 26 x 4 numpy matrices that will hold the variance and mean for each feature of each letter. My question is how to do I find the mean and variance for a specific letter and specific column? Also is there a better way to do this than going from the dataframe to matrix or is this an ok way to go about it?



Output I'd be expecting for A would be 3.5 for column one, 1.5 for column 2, 3 for column 3 and 2 for column 4. I would append each of these to a numpy matrix. Eventually the matrix I'd like to generate would look something like:



[[3.5, 1.5, 3, 2]
[1, 3, 5, 2]
[2, 8, 9, 1]]


So the first array are the means of letter A column 1-4, the next array are the means of letter B column 1-4, then letter C columns 1-4. For my actual dataset this would include all 26 letters.



Edit: Honest question, why am I getting downvotes for this? I googled this question and couldn't find any specific answers.










share|improve this question
























  • how is using np.mean(axis=...) a problem?

    – Julien
    Nov 15 '18 at 1:25












  • That won't give me the mean of the specific letter right? I need the mean for column 1 for letter 'A', then the mean for column 1 for letter 'B', etc. until I have the means for each letter and every column. So in total I'd have 112 means.

    – jj2593
    Nov 15 '18 at 1:30
















-1















I'm loading in a file to a pandas dataframe that looks something like:



A 3 2 4 1
B 1 3 5 2
C 2 8 9 1
A 4 1 2 3


I converted the dataframe to a numpy matrix because I'd like to store each mean and variance in separate 26 x 4 numpy matrices that will hold the variance and mean for each feature of each letter. My question is how to do I find the mean and variance for a specific letter and specific column? Also is there a better way to do this than going from the dataframe to matrix or is this an ok way to go about it?



Output I'd be expecting for A would be 3.5 for column one, 1.5 for column 2, 3 for column 3 and 2 for column 4. I would append each of these to a numpy matrix. Eventually the matrix I'd like to generate would look something like:



[[3.5, 1.5, 3, 2]
[1, 3, 5, 2]
[2, 8, 9, 1]]


So the first array are the means of letter A column 1-4, the next array are the means of letter B column 1-4, then letter C columns 1-4. For my actual dataset this would include all 26 letters.



Edit: Honest question, why am I getting downvotes for this? I googled this question and couldn't find any specific answers.










share|improve this question
























  • how is using np.mean(axis=...) a problem?

    – Julien
    Nov 15 '18 at 1:25












  • That won't give me the mean of the specific letter right? I need the mean for column 1 for letter 'A', then the mean for column 1 for letter 'B', etc. until I have the means for each letter and every column. So in total I'd have 112 means.

    – jj2593
    Nov 15 '18 at 1:30














-1












-1








-1








I'm loading in a file to a pandas dataframe that looks something like:



A 3 2 4 1
B 1 3 5 2
C 2 8 9 1
A 4 1 2 3


I converted the dataframe to a numpy matrix because I'd like to store each mean and variance in separate 26 x 4 numpy matrices that will hold the variance and mean for each feature of each letter. My question is how to do I find the mean and variance for a specific letter and specific column? Also is there a better way to do this than going from the dataframe to matrix or is this an ok way to go about it?



Output I'd be expecting for A would be 3.5 for column one, 1.5 for column 2, 3 for column 3 and 2 for column 4. I would append each of these to a numpy matrix. Eventually the matrix I'd like to generate would look something like:



[[3.5, 1.5, 3, 2]
[1, 3, 5, 2]
[2, 8, 9, 1]]


So the first array are the means of letter A column 1-4, the next array are the means of letter B column 1-4, then letter C columns 1-4. For my actual dataset this would include all 26 letters.



Edit: Honest question, why am I getting downvotes for this? I googled this question and couldn't find any specific answers.










share|improve this question
















I'm loading in a file to a pandas dataframe that looks something like:



A 3 2 4 1
B 1 3 5 2
C 2 8 9 1
A 4 1 2 3


I converted the dataframe to a numpy matrix because I'd like to store each mean and variance in separate 26 x 4 numpy matrices that will hold the variance and mean for each feature of each letter. My question is how to do I find the mean and variance for a specific letter and specific column? Also is there a better way to do this than going from the dataframe to matrix or is this an ok way to go about it?



Output I'd be expecting for A would be 3.5 for column one, 1.5 for column 2, 3 for column 3 and 2 for column 4. I would append each of these to a numpy matrix. Eventually the matrix I'd like to generate would look something like:



[[3.5, 1.5, 3, 2]
[1, 3, 5, 2]
[2, 8, 9, 1]]


So the first array are the means of letter A column 1-4, the next array are the means of letter B column 1-4, then letter C columns 1-4. For my actual dataset this would include all 26 letters.



Edit: Honest question, why am I getting downvotes for this? I googled this question and couldn't find any specific answers.







python numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 3:09







jj2593

















asked Nov 15 '18 at 1:23









jj2593jj2593

245




245












  • how is using np.mean(axis=...) a problem?

    – Julien
    Nov 15 '18 at 1:25












  • That won't give me the mean of the specific letter right? I need the mean for column 1 for letter 'A', then the mean for column 1 for letter 'B', etc. until I have the means for each letter and every column. So in total I'd have 112 means.

    – jj2593
    Nov 15 '18 at 1:30


















  • how is using np.mean(axis=...) a problem?

    – Julien
    Nov 15 '18 at 1:25












  • That won't give me the mean of the specific letter right? I need the mean for column 1 for letter 'A', then the mean for column 1 for letter 'B', etc. until I have the means for each letter and every column. So in total I'd have 112 means.

    – jj2593
    Nov 15 '18 at 1:30

















how is using np.mean(axis=...) a problem?

– Julien
Nov 15 '18 at 1:25






how is using np.mean(axis=...) a problem?

– Julien
Nov 15 '18 at 1:25














That won't give me the mean of the specific letter right? I need the mean for column 1 for letter 'A', then the mean for column 1 for letter 'B', etc. until I have the means for each letter and every column. So in total I'd have 112 means.

– jj2593
Nov 15 '18 at 1:30






That won't give me the mean of the specific letter right? I need the mean for column 1 for letter 'A', then the mean for column 1 for letter 'B', etc. until I have the means for each letter and every column. So in total I'd have 112 means.

– jj2593
Nov 15 '18 at 1:30













1 Answer
1






active

oldest

votes


















0














This should do it but you’ll need to specify column headers and keep your data in a dataframe.



 df[column_name].iloc[row_index].mean(axis=0)





share|improve this answer























  • I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

    – jj2593
    Nov 15 '18 at 1:49











  • Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

    – grantaguinaldo
    Nov 15 '18 at 2:01











  • I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

    – jj2593
    Nov 15 '18 at 3:10











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%2f53311140%2fhow-to-calculate-mean-of-specific-rows-based-on-value-and-column-in-numpy-matrix%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














This should do it but you’ll need to specify column headers and keep your data in a dataframe.



 df[column_name].iloc[row_index].mean(axis=0)





share|improve this answer























  • I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

    – jj2593
    Nov 15 '18 at 1:49











  • Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

    – grantaguinaldo
    Nov 15 '18 at 2:01











  • I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

    – jj2593
    Nov 15 '18 at 3:10
















0














This should do it but you’ll need to specify column headers and keep your data in a dataframe.



 df[column_name].iloc[row_index].mean(axis=0)





share|improve this answer























  • I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

    – jj2593
    Nov 15 '18 at 1:49











  • Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

    – grantaguinaldo
    Nov 15 '18 at 2:01











  • I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

    – jj2593
    Nov 15 '18 at 3:10














0












0








0







This should do it but you’ll need to specify column headers and keep your data in a dataframe.



 df[column_name].iloc[row_index].mean(axis=0)





share|improve this answer













This should do it but you’ll need to specify column headers and keep your data in a dataframe.



 df[column_name].iloc[row_index].mean(axis=0)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 1:34









grantaguinaldograntaguinaldo

217




217












  • I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

    – jj2593
    Nov 15 '18 at 1:49











  • Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

    – grantaguinaldo
    Nov 15 '18 at 2:01











  • I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

    – jj2593
    Nov 15 '18 at 3:10


















  • I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

    – jj2593
    Nov 15 '18 at 1:49











  • Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

    – grantaguinaldo
    Nov 15 '18 at 2:01











  • I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

    – jj2593
    Nov 15 '18 at 3:10

















I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

– jj2593
Nov 15 '18 at 1:49





I got an error when I tried this and I'm not sure this is what I'm looking for. Maybe I'm not being very clear in my initial post. I have a dataset of around 15,000 rows. The first column has some letter A through Z. I need the mean for each column of each specific letter. So in total with 4 features I'd need 112 total means, 26 means for each column.

– jj2593
Nov 15 '18 at 1:49













Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

– grantaguinaldo
Nov 15 '18 at 2:01





Can you update your question and show an example of the output you’re looking for? What you’ve asked for and what you want seem to be different.

– grantaguinaldo
Nov 15 '18 at 2:01













I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

– jj2593
Nov 15 '18 at 3:10






I just added the output. My question is more specific to how to calculate the means, not necessarily add them to my matrix but I think my edit should give you a better idea of what I'm trying to do. I also got my code to work using the dataframe but I'd still like to know how to do it with a numpy matrix.

– jj2593
Nov 15 '18 at 3:10




















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%2f53311140%2fhow-to-calculate-mean-of-specific-rows-based-on-value-and-column-in-numpy-matrix%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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3