Add numpy array values as columns to DataFrame by unique integer











up vote
-2
down vote

favorite












If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.










share|improve this question




















  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    Nov 19 at 11:18










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    Nov 19 at 11:20








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    Nov 19 at 11:22










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – nixon
    Nov 19 at 11:24












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    Nov 19 at 11:33

















up vote
-2
down vote

favorite












If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.










share|improve this question




















  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    Nov 19 at 11:18










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    Nov 19 at 11:20








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    Nov 19 at 11:22










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – nixon
    Nov 19 at 11:24












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    Nov 19 at 11:33















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.










share|improve this question















If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.







python pandas numpy dictionary data-science






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 12:55

























asked Nov 19 at 11:15









Jeffrey Ely

24




24








  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    Nov 19 at 11:18










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    Nov 19 at 11:20








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    Nov 19 at 11:22










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – nixon
    Nov 19 at 11:24












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    Nov 19 at 11:33
















  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    Nov 19 at 11:18










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    Nov 19 at 11:20








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    Nov 19 at 11:22










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – nixon
    Nov 19 at 11:24












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    Nov 19 at 11:33










5




5




Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18




Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18












It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
– Jeffrey Ely
Nov 19 at 11:20






It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
– Jeffrey Ely
Nov 19 at 11:20






1




1




@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22




@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22












Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
– nixon
Nov 19 at 11:24






Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
– nixon
Nov 19 at 11:24














Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
– Jeffrey Ely
Nov 19 at 11:33






Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
– Jeffrey Ely
Nov 19 at 11:33














1 Answer
1






active

oldest

votes

















up vote
0
down vote













dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer























  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    Nov 21 at 8:32










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    Nov 21 at 13:53











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',
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%2f53373446%2fadd-numpy-array-values-as-columns-to-dataframe-by-unique-integer%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








up vote
0
down vote













dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer























  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    Nov 21 at 8:32










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    Nov 21 at 13:53















up vote
0
down vote













dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer























  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    Nov 21 at 8:32










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    Nov 21 at 13:53













up vote
0
down vote










up vote
0
down vote









dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer














dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 8:32









WhatsThePoint

2,11342034




2,11342034










answered Nov 21 at 7:11









Anuprita

285




285












  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    Nov 21 at 8:32










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    Nov 21 at 13:53


















  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    Nov 21 at 8:32










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    Nov 21 at 13:53
















Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32




Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32












Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
– Jeffrey Ely
Nov 21 at 13:53




Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
– Jeffrey Ely
Nov 21 at 13:53


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53373446%2fadd-numpy-array-values-as-columns-to-dataframe-by-unique-integer%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy